Core JavaScript 1.5 Guide:Creating a Regular Expression
From MDC
[edit] Creating a Regular Expression
You construct a regular expression in one of two ways:
- Using a regular expression literal, as follows:
re = /ab+c/;
- Regular expression literals provide compilation of the regular expression when the script is evaluated. When the regular expression will remain constant, use this for better performance.
- Calling the constructor function of the RegExp object, as follows:
re = new RegExp("ab+c");
- Using the constructor function provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.