Easy Tutorial
❮ Prop Element Offsetleft Prop Option Selected ❯

JavaScript RegExp Object


RegExp Object

A regular expression is an object that describes a pattern of characters.

Regular expressions are used to perform pattern-matching and "search-and-replace" functions on text. They are powerful tools for pattern matching in strings.

Syntax

Note: When using the constructor function to create a RegExp object, you need to use the regular character escape rules (precede special characters with a backslash ). For example, the following are equivalent:

var re = new RegExp("\\w+");
var re = /\w+/;

For more information about the RegExp object, please read our JavaScript RegExp Object Tutorial.


Modifiers

Modifiers are used to perform case-insensitive and global matches:

Modifier Description
i Perform case-insensitive matching.
g Perform a global match (find all matches rather than stopping after the first match).
m Perform multiline matching.

Brackets

Brackets are used to find a range of characters:

Expression Description
[abc] Find any character between the brackets.
[^abc] Find any character not between the brackets.
[0-9] Find any digit from 0 to 9.
[a-z] Find any character from lowercase a to lowercase z.
[A-Z] Find any character from uppercase A to uppercase Z.
[A-z] Find any character from uppercase A to lowercase z.
[adgk] Find any character in the given set.
[^adgk] Find any character outside the given set.
(red blue green) Find any of the specified options.

Metacharacters

Metacharacters are characters with a special meaning:

Metacharacter Description
. Find a single character, except newline or line terminator.
\w Find a word character (alphanumeric character plus underscore).
\W Find a non-word character.
\d Find a digit.
\D Find a non-digit character.
\s Find a whitespace character.
\S Find a non-whitespace character.
\b Match a word boundary.
\B Match a non-word boundary.
\0 Find a NULL character.
\n Find a newline character.
\f Find a form feed character.
\r Find a carriage return character.
\t Find a tab character.
\v Find a vertical tab character.
\xxx Find the character specified by an octal number xxx.
\xdd Find the character specified by a hexadecimal number dd.
\uxxxx Find the Unicode character specified by a hexadecimal number xxxx.

Quantifiers

Quantifier Description
n+ Matches any string that contains at least one n. For example, /a+/ matches "a" in "candy" and all "a"s in "caaaaaaandy".
n* Matches any string that contains zero or more occurrences of n. For example, /bo*/ matches "boooo" in "A ghost booooed" and "b" in "A bird warbled", but nothing in "A goat grunted".
n? Matches any string that contains zero or one occurrence of n. For example, /e?le?/ matches "el" in "angel" and "le" in "angle".
n{X} Matches any string that contains a sequence of X n's. For example, /a{2}/ does not match "a" in "candy," but matches all "a"s in "caandy," and the first two "a"s in "caaandy."
n{X,} Matches any string that contains a sequence of at least X n's. For example, /a{2,}/ does not match "a" in "candy," but matches all "a"s in "caandy" and in "caaaaaaandy."
n{X,Y} Matches any string that contains a sequence of X to Y n's. For example, /a{1,3}/ matches nothing in "cndy", "a" in "candy," the two "a"s in "caandy," and the first three "a"s in "caaaaaaandy". Note that when matching "caaaaaaandy", the match is "aaa", even though the original string had more "a"s.
n$ Matches any string with n at the end of it.
^n Matches any string with n at the beginning of it.
?=n Matches any string that is followed by a specific string n.
?!n Matches any string that is not followed by a specific string n.

RegExp Object Methods

Method Description
compile Deprecated in version 1.5. Compiles a regular expression.
exec Tests for a match in a string. It returns the matched text if it finds a match, otherwise it returns null.
test Tests for a match in a string. It returns true or false.
toString Returns the string value of the regular expression.

Methods of the String Object Supporting Regular Expressions

Method Description FF IE
search Executes the search for a match between a regular expression and a specified string. 1 4
match Finds a match between a regular expression and a string, and returns the matches. 1 4
replace Executes a search for a match between a regular expression and a string, and replaces the matched substring with a new substring. 1 4
split Splits a string into an array of substrings using a regular expression. 1 4

RegExp Object Properties

Property Description
constructor Returns the function that created the RegExp object's prototype.
global Specifies whether the "g" modifier is set.
ignoreCase Specifies whether the "i" modifier is set.
lastIndex Specifies the index at which to start the next match.
multiline Specifies whether the "m" modifier is set.
source Returns the text of the pattern.
❮ Prop Element Offsetleft Prop Option Selected ❯