Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Regular Expression Object

Regular Expression Object (JavaScript)

An object that contains a regular expression pattern along with flags that identify how to apply the pattern.

re = /pattern/[flags]
re = new RegExp("pattern"[,"flags"])


Required. The variable name to which the regular expression pattern is assigned.


Required. The regular expression pattern to use. If you use Syntax 1, delimit the pattern by "/" characters. If you use Syntax 2, enclose the pattern in quotation marks.


Optional. Enclose flag in quotation marks if you use Syntax 2. Available flags, which may be combined, are:

  • g (global search for all occurrences of pattern)

  • i (ignore case)

  • m (multiline search)

  • u (unicode), enables EcmaScript 6 Unicode features. Supported only in Microsoft Edge.

  • y (sticky match), searches for matches at the lastIndex property of the regular expression (and does not search at later indexes). Supported in Microsoft Edge with experimental JavaScript features enabled (about:flags).

The Regular Expression object should not be confused with the global RegExp object. Even though they sound the same, they are separate and distinct. The properties of the Regular Expression object contain only information about one particular Regular Expression instance, while the properties of the global RegExp object contain continually updated information about each match as it occurs.

Regular Expression objects store patterns used when searching strings for character combinations. After the Regular Expression object is created, it is either passed to a string method, or a string is passed to one of the regular expression methods. Information about the most recent search performed is stored in the global RegExp object.

Use Syntax 1 when you know the search string ahead of time. Use Syntax 2 when the search string is changing frequently, or is unknown, such as strings taken from user input.

The pattern argument is compiled into an internal format before use. For Syntax 1, pattern is compiled as the script is loaded. For Syntax 2, pattern is compiled just before use, or when the compile method is called.

The following example illustrates the use of the Regular Expression object by creating an object (re) containing a regular expression pattern with its associated flags. In this case, the resulting Regular Expression object is then used by the match method:

var s = "through the pages of the book";

// Create regular expression pattern.
var re = new RegExp("the", "i");

// Attempt match on search string.
var r = s.match(re);   

// Return first occurrence of "the".
if(console && console.log) {

// Output:

The following example updates the regular expression pattern to search for multiple instances.

// Create regular expression pattern using the i and g flags.
var re = new RegExp("the", "ig");

// Attempt match on search string.
var r = s.match(re);   

// Return the two occurrences of "the".
if(console && console.log) {

// Output:
// 2
// [object Array] ["the", "the"]

When using the /y flag, if a match succeeds, it updates the lastindex to the index of next character after the last match. If the match fails, it resets the lastindex to 0.

The following example searches for a match at a specific index using the /y flag and the lastIndex property.

// Create regular expression pattern using the i and y flags.
var re = new RegExp("the", "iy");

// Set the lastIndex property and attempt a match
// at the specified index.
re.lastIndex = 20;
var r = s.match(re);   

// No matches returned.
if(console && console.log) {
// Reset the lastIndex property and attempt a match.
re.lastIndex = 21;
var r = s.match(re);

// Return occurrence of "the" starting at index 21.
if(console && console.log) {

// Output:
// null
// [object Array] ["the"]

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported in Store apps (Windows 8 and Windows Phone 8.1). See Version Information.

The u flag is supported in Microsoft Edge.

The y flag is supported in Microsoft Edge with experimental JavaScript features enabled (about:flags).

© 2015 Microsoft