JS RegExp Object


RegExp object

A regular expression is an object that describes a character pattern.

Regular expressions are used to match and retrieve substitutions for string patterns and are powerful tools for performing pattern matching on strings.

Grammar

var patt=new RegExp(pattern,modifiers);


Or a simpler way:

var patt=/pattern/modifiers;

  • pattern describes the pattern of an expression

  • modifiers are used to specify global matching, case-sensitive matching, and multiline matching

Note: when using constructors to create regular objects, the general character escape rules (preceded by a backslash \) are required. for example, the following is equivalent:

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

For more information about RegExp objects, read our JavaScript RegExp Objects tutorial .


Modifiers

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

ModifiersDescription
iPerforms a case-insensitive match.
gPerforms a global match (finds all matches instead of stopping when the first match is found).
mPerform multi-row matching.

Square bracket

Square brackets are used to find characters in a range:

expressiondescription
[abc]Finds any character between square brackets.
[^abc]Look for any character that is not between square brackets.
[0-9]Find any number from 0 to 9.
[a-z]Finds any character from lowercase a to lowercase z.
[A-Z]Find any character from uppercase a to uppercase z.
[A-z]Finds any character from uppercase A to lowercase z.
[adgk]Finds any character within a given collection.
[^adgk]Finds any character outside the given collection.
(red|blue|green)Find any of the specified options.

Metacharacters

Metacharacter is a character with a special meaning:

MetacharacterDescription
.Find individual characters, except for line breaks and line end characters.
\wFind numbers, letters, and underscores.
\WFind non-word characters.
\dFind the number.
\DFind non-numeric characters.
\sFind whitespace characters.
\SFind non-whitespace characters.
\bMatch word boundaries.
\BMatch non-word boundaries.
\0Look for null characters.
\nLook for line breaks.
\fLook for page feeds.
\rLook for carriage returns.
\tLook up tabs.
\vFind vertical tabs.
\xxxLook for characters specified in the octal number xxx.
\xddLook for characters specified in the hexadecimal number dd.
\uxxxxLook for Unicode characters specified in the hexadecimal number xxxx.

Measure words

Measure wordDescription
n+

Matches any string that contains at least one n.

For example, /a+/ matches "a" in "candy", all "a" in "caaaaaaandy".

n*

Matches any string containing zero or more n.

For example, /bo*/ matches "boooo" in "A ghost booooed", "b" in "A bird warbled", but does not match "A goat grunted".

n?

Matches any string containing zero or one n.

For example, /e?le?/ matches "el" in "angel", and "le" in "angle".

n{X}

Matches a string that contains a sequence of X n's.

For example, /a{2}/ does not match "a" in "candy,", but matches two "a" in "caandy," and matches the first two "a" in "caaandy.".

n{X,}

X is a positive integer. The preceding pattern n matches at least X consecutive occurrences.

For example, /a{2,}/ does not match "a" in "candy", but matches all "a" in "caandy" and "caaaaaaandy.".

n{X,Y}

X and Y are positive integers. The preceding pattern n appears at least X times in a row and matches at most Y times.

For example, /a{1,3}/ does not match "cndy", matches the one "a" in "candy, matches the tow "aa" in "caandy,", and matches the first three "a" in "caaaaaaandy". Note that when "caaaaaaandy" is matched, the match is "aaa" even though the original string has more "a".

n$Matches any string that ends in n.
^nMatches any string that begins with n.
?=nMatches any string immediately following 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. compile the regular expression.
execRetrieves the value specified in the string. returns the found value and determines its location.
testRetrieves the value specified in the string. returns true or false.
toStringReturns a string of regular expressions.

A method of a String object that supports regular expressions

MethodDescriptionFFIE
searchRetrieves a value that matches a regular expression.14
matchA match was found for one or more regular expressions.14
replaceReplaces the substring that matches the regular expression.14
splitSplits a string into string arrays.14

RegExp object properties

propertyDescription
constructorReturns a function that is a prototype for creating a RegExp object.
globalDetermines whether the "g" modifier is set
ignoreCaseDetermines whether the "i" modifier is set
lastIndexUsed to specify the starting position for the next match
multilineDetermines whether the "m" modifier is set
sourceReturns the matching pattern of a regular expression