compile (JavaScript)
Recreates a RegExp object.
Defined in
RegExp (Standard - JavaScript)Syntax
compile(expression:any, flags:string)| Parameters | Description |
|---|---|
expression |
The value of the regular expression, that is, the part that falls between the forward slashes when specified as a literal. The value is empty if not specified. |
flags |
One or both of the following flags:
This parameter is required. Use an empty string for no flags. |
Usage
Remember to escape the backslash in a string literal. For example, where you would specify/\s*;\s*/ as
a regular expression literal, you must specify "\\s*;\\s*" as
a constructor parameter.Examples
(1) This example creates a regular expression that finds the first occurrence ofMoscow in
a string.var cities = new String("Paris; Moscow; Tokyo; Moscow");
var re = new RegExp("(foo)");
re.compile("(Moscow)", "");
return cities.replace(re, "Kiev")(2) This example creates
a regular expression that finds all occurrences of
Moscow in
a string.var cities = new String("Paris; Moscow; Tokyo; Moscow");
var re = new RegExp("(foo)");
re.compile("(Moscow)", "g");
return cities.replace(re, "Kiev")