A regular expression object contains the pattern of a regular expression. It has properties and methods for using that regular expression to find and replace matches in strings.
In addition to the properties of an individual regular expression object that you create using the RegExp constructor function, the predefined RegExp object has static properties that are set whenever any regular expression is used. Regular expression is a core object.
Note: | In JavaScript 1.5, some characters such as pipes (|) which were treated a characters in certain circumstances are now treated as regular expression symbols. See also http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference. |
A literal text format or the RegExp constructor function.
The literal format is used as follows:
/pattern/flags
The constructor function is used as follows:
new RegExp("pattern", "flags")
pattern
The text of the regular expression
flags
(Optional) If specified, flags can have one of the following 3 values:
Notice that the parameters to the literal format do not use quotation marks to indicate strings, while the parameters to the constructor function do use quotation marks. So the following expressions create the same regular expression:
When using the constructor function, the normal string escape rules (preceding special characters with \ when included in a string) are necessary. For example, the following are equivalent:
The following table provides a complete list and description of the special characters that can be used in regular expressions.
The literal notation provides compilation of the regular expression when the expression is evaluated. Use literal notation when the regular expression will remain constant. For example, if you use literal notation to construct a regular expression used in a loop, the regular expression won't be recompiled on each iteration.
The constructor of the regular expression object, for example, new RegExp("ab+c"), 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. Once you have a defined regular expression, and if the regular expression is used throughout the script and may change, you can use the compile method to compile a new regular expression for efficient reuse.
A separate predefined RegExp object is available in each window; that is, each separate thread of JavaScript execution gets its own RegExp object. Because each script runs to completion without interruption in a thread, this assures that different scripts do not overwrite values of the RegExp object.
The predefined RegExp object contains the static properties input, multiline, lastMatch, lastParen, leftContext, rightContext, and $1 through $9. The input and multiline properties can be preset. The values for the other static properties are set after execution of the exec and test methods of an individual regular expression object, and after execution of the match and replace methods of String.
The following script uses the replace method to switch the words in the string. For the replacement text, the script uses the values of the $1 and $2 properties of the global RegExp object. Note that the RegExp object name is not be prepended to the $ properties when they are passed as the second argument to the replace method.
"Smith, John".
In the following example, RegExp.input is set by the Change event. In the getInfo function, the exec method uses the value of RegExp.input as its argument. Note that RegExp is prepended to the $ properties.