Availability: |
| |||||||
JavaScript syntax: | - | myRegExp.compile(aPattern) | ||||||
- | myRegExp.compile(aPattern, someAttribs) | |||||||
Argument list: | aPattern | A regular expression-matching pattern contained in a string | ||||||
someAttribs | A string containing the regular expression attributes |
This takes the same argument as the constructor and can be used to recycle an existing RegExp object with a freshly initialized search pattern and attributes.
The first argument is a string containing a properly escaped pattern. Because the backslash character (\) is an escape when used in a string, for the backslashes to remain in the regular expression pattern, they will need to be double escaped. For example, this pattern:
/\d+/
Must be escaped like this when used in a string:
"\\d+"
The second argument is the attributes for the pattern. You should not put the attributes on the end of the pattern but specify them separately. These are available:
Operator | Description | JavaScript |
---|---|---|
i | Ignore case | JavaScript 1.2 |
g | Match globally | JavaScript 1.2 |
ig | Ignore case and match globally | JavaScript 1.2 |
m | Multiple line parsing | JavaScript 1.5 |
The regular expression compile() method is useful for those occasions when you cannot easily predict what string you will need to match. As is the case with the constructor you can define the pattern based on a user-specified value. With the compile() method, you can replace the pattern as needed without destroying and recreating the object again.
Calling the compile() method on a RegExp object is a good way to gain some performance improvements if the regular expression is used frequently during a session. It will need compiling again each time it is instantiated.
Prev | Home | Next |
RegExp.$n | Up | RegExp.constructor |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |