Compiles a regular expression into an internal format for faster execution.
rgExp.compile(pattern, [flags])
The compile method converts pattern into an internal format for faster execution. This allows for more efficient use of regular expressions in loops, for example. A compiled regular expression speeds things up when reusing the same expression repeatedly. No advantage is gained, however, if the regular expression changes.
The following example illustrates the use of the compile method:
function CompileDemo(){ var rs; var s = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPp" // Create regular expression for uppercase only. var r = new RegExp("[A-Z]", "g"); var a1 = s.match(r) // Find matches. // Compile the regular expression for lowercase only.r.compile(
"[a-z]",
"g")
; var a2 = s.match(r) // Find matches. return(a1 + "\n" + a2; }
Regular Expression Object Methods | Regular Expression Object Properties | Regular Expression Syntax
Applies To: Regular Expression Object