RegExp (RegExp - JavaScript)

Creates a new RegExp object.

Defined in

RegExp (Standard - JavaScript)

Syntax

RegExp(expression:string)

RegExp(expression:string, 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.
flags One or both of the following flags:
  • g to apply the expression globally
  • i to apply the expression case insensitive

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 of Moscow 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")