JavaScript RegExp Patterns
New to RegExp?
// or
let regext = new RegExp(pattern, flags)
RexExp Flags / Modifiers
Flags can be added to a regexp pattern to modify its behavior:
Flag | Description |
---|---|
/d | Performs substring matches (new 2022) |
/g | Performs a global match (find all) |
/i | Performs case-insensitive matching |
/m | Performs multiline matching |
/s | Allows . (dot) to match line terminators (new 2018) |
/u | Enables Unicode support (new 2015) |
/v | An upgrade to the /u flag for better Unicode support (new 2025) |
/y | Performs a "sticky" search (new 2015) |
RexExp Character Classes
A character class is one or more characters enclosed in square brackets:
Class | Description |
---|---|
[a] | Matches the character between the brackets |
[^a] | Matches all characters NOT between the brackets |
[abc] | Matches all characters between the brackets |
[^abc] | Matches all characters NOT between the brackets |
[a-z] | Matches all characters in the range from a to z |
[^a-z] | Matches all characters NOT in the range from a to z |
[0-9] | Matches all characters in the range from 0 to 9 |
[^0-9] | Matches all characters NOT in the range from 0 to 9 |
RexExp Metacharacters
Metacharacters are characters with a special meaning:
RexExp Assertions
Assertions include boundaries, which indicate the beginnings and endings of lines and words:
Char | Description |
---|---|
^ | Matches from beginning of a string, or the beginning of a line if the m (multiline) flag is set |
$ | Matches from the end of a string, or the end of a line if the m (multiline) flag is set |
\b | Matches from the beginning or end of a word |
\B | Matches NOT from the beginning or end of a word |
(?=...) | Matches the subsequent string |
(?!...) | Matches NOT the subsequent string |
(?<=...) | Matches the previous string (new 2018) |
(?<!...) | Matches NOT the previous string (new 2018) |
RexExp Groups
Regular Expression Groups (aka capturing groups) allows parts of a matched string to be extracted and reused. They are created by enclosing a pattern within parentheses ():
Char | Description |
---|---|
(x) | Matches x and remembers the match |
(?<n>x) | Matches x and labels it n |
(?flag:x) | Enables flag(s) for x |
(?flag-flag:x) | Disables flag(s) for x |
RexExp Quantifiers
Quantifiers indicate the numbers of characters or expressions to match:
Code | Description |
---|---|
x+ | Matches at least one x |
x* | Matches zero or more occurrences of x |
x? | Matches zero or one occurrences of x |
x{n} | Matches n occurences of x |
x{n,m} | Matches from n to m occurences of x |
x{n,} | Matches n or more occurences of x |
Browser Support
/regexp/
is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |