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?
Asked
Active
Viewed 416 times
-1
-
1... compilation at runtime. It’s a string, not a regex, so the regex is created from the string at runtime. Immediate regexes don’t change, so they’re compiled during parsing. – Dave Newton Feb 15 '20 at 03:39
1 Answers
3
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);

izambl
- 629
- 3
- 9