YouTip LogoYouTip

Jsref Obj Regexp

```html JavaScript RegExp Object | Rookie Tutorial

RegExp Object

Regular expressions are objects that describe character patterns.

Regular expressions are used for pattern matching and search/replace on strings. They are a powerful tool for performing pattern matching on strings.

Syntax

var patt=new RegExp(pattern,modifiers);

Or a simpler way:

var patt=/pattern/modifiers;
  • pattern describes the pattern of the expression
  • modifiers are used to specify global matching, case-insensitive matching, and multiline matching

Note: When using the constructor to create a regular expression object, normal character escape rules apply (prepend with backslash ). For example, the following are equivalent:

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

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


Modifiers

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

ModifierDescription
iPerform case-insensitive matching.
gPerform global matching (find all matches rather than stopping after the first match).
mPerform multiline matching.

Brackets

Brackets are used to find characters within a range:

ExpressionDescription
Find any character between the brackets.
[^abc]Find any character not between the brackets.
Find any digit from 0 to 9.
Find any character from lowercase a to lowercase z.
Find any character from uppercase A to uppercase Z.
Find any character from uppercase A to lowercase z.
Find any character within 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 special meanings:

MetacharacterDescription
.Find a single character, except newline or line terminator.
wFind word characters (digits, letters, underscore).
WFind non-word characters.
dFind digits.
DFind non-digit characters.
sFind whitespace characters.
SFind non-whitespace characters.
bMatch a word boundary.
BMatch a non-word boundary.
Find the NULL character.
nFind a newline character.
fFind a form feed character.
rFind a carriage return character.
tFind a tab character.
vFind a vertical tab character.
xxxFind the character specified by octal number xxx.
xddFind the character specified by hexadecimal number dd.
uxxxxFind the Unicode character specified by hexadecimal number xxxx.

Quantifiers

QuantifierDescription
n+Matches any string that contains at least one n. For example, /a+/ matches the "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", "b" in "A bird warbled", but not "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 a sequence of X occurrences of n. For example, /a{2}/ does not match the "a" in "candy", but matches the two "a"s in "caandy", and the first two "a"s in "caaandy".
n{X,}X is a positive integer. Matches when the preceding pattern n occurs at least X times consecutively. For example, /a{2,}/ does not match "a" in "candy", but matches all "a"s in "caandy" and "caaaaaaandy".
n{X,Y}X and Y are positive integers. Matches when the preceding pattern n occurs at least X times and at most Y times consecutively. For example, /a{1,3}/ does not match "cndy", matches "a" in "candy", two "a"s in "caandy", and the first three "a"s in "caaaaaaandy". Note that when matching "caaaaaaandy", even though the original string has more "a"s, the match is "aaa".
n$Matches any string that ends with n.
^nMatches any string that starts with n.
?=nMatches any string that is immediately followed by the specified string n.
?!nMatches any string that is not immediately followed by the specified string n.

RegExp Object Methods

MethodDescription
compileDeprecated in version 1.5. Compiles a regular expression.
execRetrieves the specified value in the string. Returns the found value and determines its position.
testRetrieves the specified value in the string. Returns true or false.
toStringReturns the string representation of the regular expression.

String Object Methods Supporting Regular Expressions

MethodDescriptionFFIE
searchRetrieves the value that matches the regular expression.14
matchFinds one or more matches of the regular expression.14
replaceReplaces substrings that match the regular expression.14
splitSplits a string into an array of strings.14

RegExp Object Properties

PropertyDescription
constructorReturns a function that is the prototype for creating RegExp objects.
globalChecks whether the "g" modifier is set.
ignoreCaseChecks whether the "i" modifier is set.
lastIndexSpecifies the starting position for the next match.
multilineChecks whether the "m" modifier is set.
sourceReturns the matching pattern of the regular expression.
```
← Jsref Obj GlobalJsref Obj String β†’