javascriptcompilationruntimeruntime-compilation

What is "runtime compilation" in Javascript?


I'm reading this article on regular expressions, and it says "Using the constructor function provides runtime compilation of the regular expression". It then says "Use the constructor function when you know the regular expression pattern will be changing". What I want to know is, what exactly is runtime compilation in Javascript?


Solution

  • It means that when you use the /ab+c/; the regexp es compiled when the script is loaded and before it's used but if you use the constructor it is compiled just before that line is executed.

    Using the constructor is useful, for example, when you want to build the regexp using a variable: new RegExp('ab+' + value);