Regex
- Flags
- Character Classes
-
Quantifiers (
*
,+
,?
,{n}
,{n,}
,{n,m}
) - Assertions
- Anchors
-
Character set or range
[]
and negated character set[^]
- Examples
- JavaScript examples
- Additional Resources
Flags
-
g
: Global. Searches the entire string. Withoutg
, only the first match is returned. -
i
: Case Insensitive. Makes the entire regex case-insensitive. -
m
: Multiline. Causes^
and$
to match the start and end of lines, not just the start and end of the string. -
s
: Single line (DotAll). Causes.
to match newline characters as well. -
u
: Unicode. Treats the pattern as a sequence of Unicode code points. -
y
: Sticky. Matches at the exact position in the string specified by thelastIndex
property (useful with loops).
Character Classes
-
.
Matches any character (except newline) -abcABC123 .:!?
-
\d
Matches any digit (0-9) -1234567890
-
\D
Matches any non-digit character -abcABC_~!@#$%^&*()-+=[]{}|;:',.<>/?
-
\w
Matches any word character (letters, numbers, underscore) -abcABC_123
-
\W
Matches any non-word character -~!@_#$%^&*()-+=[]{}|;:',.<>
-
\s
Matches any whitespace character (spaces, tabs, line breaks) -
\S
Matches any non-whitespace character (any visible character) -
\b
Matches a word boundary -\bcat\b
matchescat
-
\B
Matches a non-word boundary -
\\
Escapes special characters
A word character is defined as any alphanumeric character (a-z, A-Z, 0-9) or an underscore (_).
*
, +
, ?
, {n}
, {n,}
, {n,m}
)
Quantifiers (-
*
Zero or more -\be*\
matchesb
be
bee
-
+
One or more -\be+\
matchesbe
bee
-
?
Zero or one -\be*\
matchesb
be
-
{n}
Exactly n times -\be{2}\
matchesbee
-
{n,}
n or more times -\be{2,}\
matchesbee
beee
-
{n,m}
Between n and m times -\be{1,2}\
matchesbe
bee
Assertions
Assertions are conditions that do not consume characters in the string, but they assert whether a match is possible or not. They are used to test if a string contains a certain pattern without including the pattern in the result.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Assertions
All examples below are referring to the string as abc123
-
(?=)
Positive lookahead -x(?=y)
matchesx
only when followed byy
-
(?!)
Negative lookahead -x(?!y)
matchesx
only when NOT followed byy
-
(?<=)
Positive lookbehind -(?<=y)x
matchesx
only when preceded byy
-
(?<!)
Negative lookbehind -(?<!y)x
matchesx
only when NOT preceded byy
Anchors
-
$
End of string or line -cat$
matchescat
at the end of a string -
\A
Start of string -\Acat
matchescat
at the start of a string -
\Z
End of string -cat\Z
matchescat
at the end of a string -
\b
Matches a word boundary - In "cat dog",\bcat\b
matchescat
-
\B
Matches a non-word boundary. In "concatenate",\Bcat\B
matchescat
. -
^
Start of string or line
[]
and negated character set [^]
Character set or range Use the square brackets []
to specify a character set or range. Alternatively, use
square brackets with a caret [^]
to define a negated character set. This set will match
any character except those listed inside the brackets.
/b[aio]r/
- Finds all words that start with b
, ends with r
and has either a
, i
or o
in between.
Output: bar ber bir bor bur
/b[^eu]r/
- Finds all words that start with b
, ends with r
and has any character except a
, i
, or o
in the middle.
Negated Output: bar ber bir bor bur
/[a-f]/
- Finds all characters from a
to f
.
Output: abcdefghijklmnopqrstuvwxyz
/[^a-f]/
- Finds all characters that are not from a
to f
.
Negated Output: abcdefghijklmnopqrstuvwxyz
/[3-6]/
- Finds all numbers from 3
to 6
.
Output: 0123456789
/[^3-6]/
- Finds all characters that are not numbers from 3
to 6
.
Negated Output: 0123456789
[bdf]eer
words that start with b
, d
, or f
and end with eer
[^bdf]eer
words that do not start with b
, d
, or f
and end with eer
Examples
Search for words or character in a string
/e/g
- The quick brown fox jumps over the lazy dog.
/the/g
- The quick brown fox jumps over the lazy dog.
/the/gi
- The quick brown fox jumps over the lazy dog.
Find words that start with ...
\b[bfla]\w*
- words that start with b
, f
, l
, or a
Mike likes to ride a bike like he is five again, not fifty.
\b[bfla]\w*e
- words that start with b
, f
, l
, or a
and ends with e
Mike likes to ride a bike like he is five again, not fifty.
/\b[bdf]eer\b/g
- beer deer feer
/\b[^bdf]eer\b/g
- cheer jeer leer
Find all words except for. (Negated character set)
/\b[^t]he\b/g
- The quick brown fox jumps over the lazy dog.
/\b[^t]he\b/gi
- The quick brown fox jumps over the lazy dog.
Matching words that contain...
To find words with vowels in a text, you can use the regular expression \w*[aeiou]\w*
.
\b\w*[aeiou]\w*\b when
\w*[aeiou]\w* matched Expression
matches any word that contains at least one vowel.
Here's what each part of the regex does:
-
\b
.....\b
matches a word boundary. This means it matches the position where a word character is not followed or preceded by another word character (like spaces, line breaks, punctuation, the start/end of a string, etc.).
\w* matches zero or more word characters (a word character is a letter, number, or underscore). [aeiou] matches any vowel. \w* matches zero or more word characters again. \b matches another word boundary. So, \b\w*[aeiou]\w*\b will match any word that contains at least one vowel, like "apple", "orange", "banana", etc.
JavaScript examples
Mozilla #using_regular_expressions_in_javascript
const text = "The quick brown fox jumps over the lazy dog.";
const regex = /e/g;
console.log(text.match(regex)); // ["e", "e", "e", "e", "e"]
Test for a valid Australian phone number
To pass the test, the phone number must start with 04
followed by 8 digits.
function isValidAustralianMobile(number) {
const sanitizedNumber = number.replace(/\D/g, '');
return /^04\d{8}$/.test(sanitizedNumber);
}
The sanitizedNumber
variable strips non-digit characters from the input, allowing the
function to accept phone numbers in various formats, provided they start with 04
and
are followed by 8 digits.
-
^04
asserts the start of a line begins with exactly 04. -
\d{8}
matches exactly eight digits. -
$
asserts the end of a line.