February 9, 2000 - Compiling Regular Expressions
![]() |
February 9, 2000 Compiling Regular Expressions Tips: February 2000
Yehuda Shiran, Ph.D.
|
compile() method. Its syntax is:
reg.compile("PATTERN", ["g"|"i"|"gi"])
where reg is the name of a regular expression variable, and PATTERN is the text of the regular expression.
Use the compile() method with a regular expression variable that was created with the constructor function (not the literal notation). Use the compile() method when you know the regular expression will remain constant (after getting its pattern) and will be used repeatedly throughout the script. This method actually converts the specified pattern into its internal format, for faster execution.
The compile() method can also be used to change a regular expression during execution:
var reg = new RegExp("Bart", "i"); // reg matches Bart here
reg.compile("Lisa", "i") // reg does not match Bart here
You can also use the compile() method to modify a regular expression's modifier:
var reg = new RegExp("bart", "i"); // reg matches Bart here
reg.compile("bart"); // reg does not match Bart hereLearn more about regular expressions in Column 5, Regular Expressions.


Find a programming school near you