JavaScript » Objects » RegExp

Syntax:
new RegExp("pattern"[, "flags"])

 The RegExp object contains the pattern of a regular expression, and is used to match strings using its methods and properties.

The predefined RegExp object has static properties set whenever a regular expression is used, as well as user-defined ones for individual objects. A RegExp object can be created in two ways: either by using the constructor function, or with a literal text format. In both cases you need to specify the text pattern of the regular expression, and optionally one of the three possible flags: 'g' for a global match, 'i' to ignore case, or 'gi' for a case-insensitive global match.

The literal notation of a regular expression provides compilation of it when the expression is evaluated, and this is used when you know that a regular expression is going to remain constant. When you know that an expression is going to change, or you don't know what that expression is because it will be input by a user, you need to use the constructor function which is compiled at run time. You can, however, compile a regular expression at any time using the compile method. Each window has its own predefined RegExp object thus ensuring that different threads of JavaScript execution don't overwrite values of the RegExp object.
 
For a complete list and description of the special characters that can be used in a regular expression, see the special characters page.

Examples

Code:
myRegExp = new RegExp("^a", "i")
Explanation:

This example creates a regular expression using the constructor function, that matches an initial letter 'a' irrespective of case.

Code:
myRegExp = new RegExp("\\t")
Explanation:

Note that when using the constructor function, you must use quotation marks to indicate strings. Also, the normal string escape rules apply and you must use a back slash before special characters as in the following code which creates a regular expression of a tab character.

Code:
myRegExp = /^a/i
Explanation:

Using the literal text format you do not need to use quotation marks. To create a regular expression consisting of a case-insensitve initial 'a' you could use this.

Code:
myRegExp = /\t/
Explanation:

...and a regular expression consisting of a tab character could be created like this.

Code:
new RegExp("\\(name", "gi"), ("Tbl.Name");
Explanation:

Here is a regular expression to replace a string that starts with a left parenthesis.

Properties

$

See the input property.

$&

See the lastMatch property.

$'

See the rightContext property.

$*

See the multiline property.

$+

See the lastParen property.

$`

See the leftContext property.

$1, ..., $9

These are properties containing parenthized substrings (if any) from a regular expression.

global

Syntax: object.global

This property reflects whether the 'g' flag was used to match a regular expression globally in a string, or just the first occurrence of it. Its value is true if the 'g' flag was used and false if not. Note that this property is read-only but that calling the compile method does alter it.

ignoreCase

Syntax: object.ignoreCase

This property reflects whether the 'i' flag was used for a case-insensitive match of a regular expression in a string, returning true if it was and false if not. Note that this property is read-only but that calling the compile method does alter it.

input

Syntax: RegExp.input

This property is a string against which a regular expression is matched.

lastIndex

This property is an integer that specifies the index at which to start the next match, but is only set if the regular expression uses the 'g' flag to specify a global search.

lastMatch

Syntax: RegExp.lastMatch

This property is the last matched characters. As this property is static, you always use RegExp.lastMatch.

lastParen

Syntax: RegExp.lastParen

This property contains the last matched parenthesized substring (if any), and as a static property is always refered to using RegExp.lastParen.

leftContext

Syntax: RegExp.leftContext

This property is the substring upto the character most recently matched; i.e. everything that comes before it, and as a static property, is always used as RegExp.leftContext.

multiline

Syntax: RegExp.multiline

This property reflects whether a search is to be carried out over multiple lines, returning true if it is, and false if not. Being a static property, you always use RegExp.multiline. When an event handler is called for a TEXTAREA form element, the browser sets the multiline property to true. Once the event handler has finished executing, it is reset to false, even if it was set at true before the event handler was called.

prototype

Syntax: RegExp.prototype

This property represents the prototype for this class, and allows you to add your own properties and methods to all instances of it. See the Function.prototype property.

rightContext

Syntax: RegExp.rightContext

This property is the substring after the character most recently matched; i.e. everything that follows it, and as a static property, is always used as RegExp.rightContext.

source

Syntax: object.source

This is a read-only property containing the source of the regular expression: i.e. everything except the forward slashes and any flags. The source property cannot be changed directly, however calling the compile method does alter it. For example, with the regular expression rexp = /[^aeiou\s]{2}/g the value of the source property would be [^aeiou\s]{2}.

Methods

compile

Syntax: object.compile(pattern[, flags])

This method compiles a regular expression object during execution of a script.

exec

Syntax: object.exec([str])

This method executes a search for a match in a specified string, returning a result array.

test

Syntax: object.test([str])

This method tests for a match of a regular expression in a string, returning true if successful, and false if not.

See Also: