JavaScript Regular Expressions


Regular Expressions, sometimes referred to as "regex" or "RegExp," are text strings that have particular formatting and are used to search for patterns in text. One of the most potent techniques for effective and efficient text processing and modification nowadays is the use of regular expressions. For instance, it may be used to locate or replace matching strings inside text content, as well as to check whether the format of data given by the user such as name, email, phone number, etc.

Function Description
exec() Find a match within a string. If there is a mismatch, it produces a list of data or null.
test() Check to see whether a string fits a pattern. Returning either true or false.
search() Find a match within a string via searching. It gives -1 if no matches were found or the index of the first match.
replace() a string is searched for a match, and the matched substring is replaced with a replacement string.
match() Find a match within a string. If there is a mismatch, it produces a list of data or null.
split() uses a regular expression to split a string into an array of substrings.

Defining Regular Expressions

Regular expressions are represented in JavaScript through the RegExp object, which is a native JavaScript object much like the String and Array objects. The literal syntax and the RegExp() constructor are the two methods available for constructing new RegExp objects. The literal syntax for regular expressions is shorter and simpler to understand. Consequently, using literal syntax is preferred.

Example

Preview

Pattern Matching with Regular Expression

Letters, numbers, punctuation, and a particular set of regular expression characters are all used in regular expression patterns (do not confuse them with the HTML special characters). In a regular expression, the following characters have particular meaning:. *? + [] () $ |. If you wish to use these characters literally, you must backslash them. For instance, you would need to write. if you wanted to match ".". The meanings of all other characters are taken to be literal by default.


Character Classes

Character classes, like [abc], are groups of characters surrounded by square brackets. The expression [abc] only matches the characters a, b, or c since a character class only ever matches one character from a list of defined characters. It is also possible to establish character classes that match all characters except those in brackets. A caret () sign is used to establish a class of negated characters, such as [abc], which matches all characters except for a, b, and c. The hyphen (-) symbol can be used to specify a range of characters inside a character class like [0-9].

RegExp Description
[abc] any one of the letters a, b, or c matches.
[^abc] any character other than a, b, or c that matches.
[a-z] matches any single lowercase letter from a through z.
[A-Z] matches any one uppercase character, from a to z.
[a-Z] each single character, from lowercase a to capital Z, is matched.
[0-9] A single digit between 0 and 9 is matched.
[a-z0-9] single character between a and z, or between 0 and 9, is matched.

Example

Preview

Predefined Character Classes

There exist shortcut names for several character classes, including numerals, letters, and whitespace, since they are so often used.

Shortcut Description
. each single character, except newline (n), is matched.
\d every digit character is compatible. similar to [0-9].
\D any non-digit character is matched. similar to [0-9].
\s any whitespace character is a match (space, tab, newline or carriage return character). similar to [tnr]
\S any non-whitespace character is matched. similar to [ tnr]
\w any word character matches (definned as a to z, A to Z,0 to 9, and the underscore). similar to [a-zA-Z 0-9]
\W any non-word character is matched. identical to [a-zA-Z 0-9]

Example

Preview

Repetition Quantifiers

We learned several different ways to match a single character in the previous section. What happens, though, if you wish to match on several characters? Here is where quantifiers are useful. You may tell a regular expression how many times a character should match by using quantifiers. Quantifiers can be used to describe the characters individually, as well as character classes and character groupings that are enclosed in parenthesis. Using JavaScript's split() function, the regular expression in the example below will divide the text at any comma, series of commas, whitespace, or any combination of these.

RegExp Description
p+ A minimum of one instance of the letter p must match.
p* matches words that include the letter p zero or more times.
p? meets 0 or 1 instances of the letter p.
p{2} precisely two instances of the letter p are matched.
p{2,3} matches the letter "p" at least twice, but not more than three times.
p{2,} matches words that include the letter p two or more times.
p{,3} only matches the letter p up to three times.

Example

Preview

Position Anchors

In some circumstances, you wish to match at the start or end of a word, string, or line. Anchors can be used for this. The dollar ($) symbol and the caret(), which denote the beginning and end of the string, respectively, are two popular anchors.

RegExp Description
^p Identifies a line that starts with the letter p.
p$ Identifies a line's last letter, which is p.

Example

Preview

Pattern Modifiers (Flags)

You may manage how a pattern match is handled with a pattern modifier. Pattern modifiers are used right after the regular expression; for instance, you may use the I modifier to search for a pattern without considering the case, as in /pattern/i.

Modifier Description
g Search for all occurrences by doing a global match.
i creates a match that ignores the case.
m changes how and $ behave such that they now match against new line boundaries instead of string boundaries, which are the beginning or end of each line in a multiline string.
o just once evaluates the expression.
s alters the way that behaves. To match any character, including newlines, use (dot).
x enables the use of whitespace and comments to improve the clarity of regular expressions.

Example

Preview

Alternation

Using alternation, you may define an alternate pattern. A regular expression's alternation function functions similarly to an if-else conditional statement's OR operator.

Example

Preview

Grouping

Like mathematical expressions, regular expressions arrange their sub expressions using parentheses. A repeat quantifier may be applied to an entire subexpression by using parentheses.

Example

Preview