RegExp (RegExp - JavaScript)
Creates a new RegExp
object.
Defined in
RegExp (Standard - JavaScript)Syntax
RegExp(expression: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. |
flags |
One or both of the following 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.A constructor of the form new RegExp("expression",
"flags")
is equivalent to a regular expression literal of
the form /expression/flags
.
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("(Moscow)");
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("(Moscow)", "g");
cities.replace(re, "Kiev")
(3) This example specifies
the regular expression as a literal instead of using a constructor.
var cities = new String("Paris; Moscow; Tokyo; Moscow");
var re = /(Moscow)/g;
cities.replace(re, "Kiev")