This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

RegExp

Summary

An intrinsic global object that stores information about the results of regular expression pattern matches.

Syntax

RegExp.property

Examples

The following example performs a regular expression search. It displays matches and submatches from the global RegExp object, and from the array that is returned by the exec method.

var newLine = "<br />";

 var re = /(\w+)@(\w+)\.(\w+)/g
 var src = "Please send mail to george@contoso.com and someone@example.com. Thanks!"

 var result;
 var s = "";

 // Get the first match.
 result = re.exec(src);

 while (result != null) {
     // Show the entire match.
     s += newLine;

     // Show the match and submatches from the RegExp global object.
     s += "RegExp.lastMatch: " + RegExp.lastMatch + newLine;
     s += "RegExp.: " + RegExp. + newLine;
     s += "RegExp.: " + RegExp. + newLine;
     s += "RegExp.: " + RegExp. + newLine;

     // Show the match and submatches from the array that is returned
     // by the exec method.
     for (var index = 0; index < result.length; index++) {
         s +=  index + ": ";
         s += result[index];
         s += newLine;
     }

     // Get the next match.
     result = re.exec(src);
 }
 document.write(s);

 // Output:
 //  RegExp.lastMatch: george@contoso.com
 //  RegExp.: george
 //  RegExp.: contoso
 //  RegExp.: com
 //  0: george@contoso.com
 //  1: george
 //  2: contoso
 //  3: com

 //  RegExp.lastMatch: someone@example.com
 //  RegExp.: someone
 //  RegExp.: example
 //  RegExp.: com
 //  0: someone@example.com
 //  1: someone
 //  2: example
 //  3: com

Remarks

The required property argument can be any one of the RegExp object properties.

The RegExp object cannot be created directly, but is always available for use. Until a successful regular expression search has been completed, the initial values of the various properties of the RegExp object are as follows:

PropertyShorthandInitial Value
index-1(none)
input$_Empty string.
lastIndex-1(none)
lastMatch$&Empty string.
lastParen$+Empty string.
leftContext$`Empty string.
rightContext$’Empty string.
$1 - $9$1 - $9Empty string.

Its properties have undefined as their value until a successful regular expression search has been completed.

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

Properties

||
|global|Returns a Boolean value indicating the state of the global flag ( g ) used with a regular expression.|
|ignoreCase|Returns a Boolean value indicating the state of the ignoreCase flag ( i ) used with a regular expression.|
|multiline|Returns a Boolean value indicating the state of the multiline flag ( m ) used with a regular expression.|
|source|Returns a copy of the text of the regular expression pattern.|
|sticky|Returns a Boolean value indicating the state of the sticky flag ( y ) used with a regular expression.|
|unicode|Returns a Boolean value indicating the state of the Unicode flag (u) used with a regular expression.|

Methods

||
|compile|Compiles a regular expression into an internal format for faster execution.|
|exec|Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.|
|test|Returns a Boolean value that indicates whether or not a pattern exists in a searched string.|

Properties

The following table lists the properties of the RegExp object.

PropertySummary
globalReturns a Boolean value indicating the state of the global flag ( g ) used with a regular expression. Default is false. Read-only.
ignoreCaseReturns a Boolean value indicating the state of the ignoreCase flag ( i ) used with a regular expression. Default is false. Read-only.
multilineReturns a Boolean value indicating the state of the multiline flag ( m ) used with a regular expression. Default is false. Read-only.
sourceReturns a copy of the text of the regular expression pattern. Read-only. The rgExp argument is a Regular expression object. It can be a variable name or a literal.
stickyReturns a Boolean value indicating the state of the sticky flag ( y ) used with a regular expression. Default is false. Read-only.
unicodeReturns a Boolean value indicating the state of the Unicode flag (u) used with a regular expression. Default is false. Read-only.

Functions

The following table lists the functions of the RegExp object.

Methods

The following table lists the methods of the RegExp object.

MethodSummary
compileCompiles a regular expression into an internal format for faster execution.
execExecutes a search on a string using a regular expression pattern, and returns an array containing the results of that search.
testReturns a Boolean value that indicates whether or not a pattern exists in a searched string.

See also

Other articles

Attributions

  • Microsoft Developer Network: Article