You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This tutorial is a quick-reference for common regular expressions, or regex, components, and how to use them.
Summary
Regular expressions are used to validate input by searching for (matching) specific patterns in a given string. This is helpful in many applications, such as to check if an email input is the correct format of an email, or if a user enters a URL that matches the correct format of a URL, etc.
Although they may seem confusing at first, with practice they become easy to work with and can be very powerful in making your code less succeptible to user error.
Capturing means saving the collected value in order to use it later.
Character Classes
Operator
Description
Match Example
\d
matches a single digit character
0...9
\D
matches a single non-digit character
\w
matches a word character
a...z, 0...9, _
\W
matches a non-word character
\s
matches a whitespace charachter (includes tabs and line-breaks)
\S
matches a non-whitespace charachter
.
matches any character
The backslash (\) is used to escape the literal characters.
Tabs are symbolized with \t
Line-breaks are symbolized with \n (for new-lines) on most OS. However, old Mac OS expects \r (for carriage return) and windows expects \r\n.
Flags
Flag
Meaning
Pattern
Description
Match Example
g
global
/abc/g
Continues subsequent searching from the end of the previous match, instead of returning after the first match
abcdefabcghiabc, etc
m
multi-line
/^abc/m
makes ^ and $ match the start and end of a line, instead of the whole string
abcdef
i
insensitive
/aBc/i
Makes the whole expression case-insensitive
AbC
Grouping and Capturing
Operator
Pattern
Description
()
a(bc)
Creates a capturing group with value bc
?:
a(?:bc)*
Disables the capturing group
?<foo>
a(?<foo>bc)
Names the group. In this case it names it foo
This operator allow us to capture the data in an array and access the values using the index of the match.
The names the groups are they keys of each group.
Bracket Expressions
Operator
Pattern
Description
Match Example
[]
[abc]
same as a|b|c
a, a b, a c
same
[a‑c]
same as above
same as above
same
[a‑zA‑Z0‑9]
any alphanumeric character
a...z, A...Z, 0...9
same
[^a‑zA‑Z]
^ negates the expression. Matches a string with no characters from a to z nor from A to Z
Special characters, such as \, do not work inside brackets.
Greedy and Lazy Match
Operator
Description
<.+?>
matches any character, as many times, between < and >
<[^<>]+>
matches any character, except < or >, as many times, between < and >
Greedy operators (* + {}) expand the match as far as possible.
? makes them lazy.
Boundaries
Operator
Pattern
Description
\b
\babc\b
Matches whole words only (in this example, the word is abc). For example, one side is a space and the other is the start or end of a string or line
\B
\Babc\B
Negation of above. So, it matches the pattern only when surrounded by word characters
Back-references
Operator
Pattern
Description
\1
([abc])\1
matches the same text that was matched by the 1st capturing group
([abc])([de])\2\1
matches the same text that was matched by the second capturing group
(?<foo>[abc])\k<foo>
names the group foo then references it later. Same as \1
Look-ahead and Look-behind
Operator
Pattern
Description
(?=)
a(?=b)
matches an a only if it is followed by a b
(?!r)
a(?!b)
(negation of above) matches an a only if is not followed by a b
(?<=)
(?<=b)a
matches an a only if it follows a b
(?<!r)
(?<!b)a
(negation of above) matches an a only if it does not follow a b
All 4 scenarios above will not capture the b in the regex match.
Notes
Regex are usually used with the match() method, such as string.match(/[0-9]/).
Regex search patterns are delimited by two forward slash (/) characters.
Operators can be combined together.
Flags can also be combined together and can be combined with operators.
You can turn any pattern being matched into a token by enclosing the pattern in parentheses within the expression. For example, to create a token for a dollar amount, you could use (\$\d+). Each token in the expression is assigned a number from 1 to 255 going from left to right.
To make a reference to a token later in the expression, refer to it using a backslash followed by the token number. For example, when referencing a token generated by the third set of parentheses in the expression, use \3.