-1

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?

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. Commented Feb 15, 2020 at 3:39

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.