Regular expressions have many uses in any code which works with text strings (or streaming textual data). Regex find use across a wide variety of complex use cases, some examples:
colou?rfinds the similarly-spelled words color (American) and colour (Commonwealth)
[^ ]*cat[^ ,!]*finds the words containing the root “cat” in “A cat, not a dog, and the toys the cat’s scattered about — categorically a catastrophe!”
Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.
A regex has the syntax
/pattern/modifiers. Using the string
/cat/i as an example, regex terminology is:
/cat/iis a complete regular expression
catis the pattern
iis the modifier
- the forward-slashes / are the pattern delimiters
search() function takes a regular expression to search for a match, and returns the position of the match.
replace() function takes a regular expression and returns a modified string where the pattern is replaced.
search() function takes a regex and returns the position within the string where the match is found (or a -1 if not found). For example, here we search through
string for “Cat” with the
imodifier (ignore case during the search).
let regex = /Cat/i ; // case-insensitive search for "Cat"let string = "my cat, Cat" ; // search in this stringconsole.log( string.search( regex ) ) ; // --> 3
Equivalent to the above,
search() may be invoked directly on any string variable:
console.log( "my cat, Cat".search(/Cat/) ) ; // --> 10console.log( "my cat, Cat".search(/Cat/i) ) ; // --> 3
/Cat/ is a very different beast than is
"Cat" in this context: the former is a regex, the latter a string. It is a frequent source of debugging rage to think one is searching with the power of regex matching only to discover a quoted string. Read on for mention of the
RegExp() function, which makes this kind of typo less common.
replace() function takes a regex and returns a string, possibly modified by the pattern if the search is successful. Compare the results of the following three calls:
Search by a quoted literal for the first occurrence:
console.log( "my cat, Cat".replace("Cat", "Dog") ) ; // --> my cat, Dog
Search by a regex, insensitively, for the first occurrence:
console.log( "my cat, Cat".replace(/Cat/i, "Dog") ) ; // --> my Dog, Cat
Search by a regex, insensitively, globally (all occurrences):
console.log( "my cat, Cat".replace(/Cat/ig, "Dog") ) ; // --> my Dog, Dog
The behavior of the regular expression matching engine is changed and extended through the use of modifiers, character ranges, class type meta-characters, and quantifiers.
Modifiers change the default matching behavior (return the first match, use case-sensitive matching, and match only the first line of a multi-line variable):
|g||global matching (all, rather first only)|
Ranges, delimited by square brackets
, match a range of characters:
|Find any character between the brackets|
|Find any character NOT between the brackets|
|Find any character between the brackets (any digit)|
|Find any character NOT between the brackets (any non-digit)|
Meta-characters match specific kinds of characters:
|a single character, except newline or line terminator|
|a word character|
|a non-word character|
|a non-digit character|
|a whitespace character|
|a non-whitespace character|
|a match at a word boundary, at the beginning with |
|a match, but not at the beginning or end of a word|