{ ILoveJS }

Regular Expressions Cheatsheet

regex

Regex syntax reference for JavaScript developers.

6 sections · 52 items

Character Classes

Character Set
[abc]

Matches any single character in the brackets

typescript
/[aeiou]/.test('hello') // true
Negated Set
[^abc]

Matches any single character NOT in the brackets

typescript
/[^0-9]/.test('abc') // true
Range
[a-z]

Matches any character in the specified range

typescript
/[A-Za-z]/.test('Hello') // true
Dot
.

Matches any single character except newline (unless s flag)

typescript
/h.t/.test('hat') // true
Word Character
\w

Matches alphanumeric character plus underscore [A-Za-z0-9_]

typescript
'hello_123'.match(/\w+/) // ['hello_123']
Non-Word Character
\W

Matches any non-word character [^A-Za-z0-9_]

typescript
'hello world'.match(/\W/) // [' ']
Digit
\d

Matches any digit [0-9]

typescript
'abc123'.match(/\d+/) // ['123']
Non-Digit
\D

Matches any non-digit character [^0-9]

typescript
'abc123'.match(/\D+/) // ['abc']
Whitespace
\s

Matches any whitespace character (space, tab, newline)

typescript
'a b'.match(/\s/) // [' ']
Non-Whitespace
\S

Matches any non-whitespace character

typescript
'  hello  '.match(/\S+/) // ['hello']

Anchors

Start of String
^

Matches the start of string (or line with m flag)

typescript
/^hello/.test('hello world') // true
End of String
$

Matches the end of string (or line with m flag)

typescript
/world$/.test('hello world') // true
Word Boundary
\b

Matches a word boundary position

typescript
/\bcat\b/.test('the cat sat') // true
Non-Word Boundary
\B

Matches a non-word boundary position

typescript
/\Bcat/.test('concat') // true

Quantifiers

Zero or More
*

Matches 0 or more of the preceding token (greedy)

typescript
'aaa'.match(/a*/) // ['aaa']
Zero or More (Lazy)
*?

Matches 0 or more of the preceding token (lazy)

typescript
'aaa'.match(/a*?/) // ['']
One or More
+

Matches 1 or more of the preceding token (greedy)

typescript
'aaa'.match(/a+/) // ['aaa']
One or More (Lazy)
+?

Matches 1 or more of the preceding token (lazy)

typescript
'aaa'.match(/a+?/) // ['a']
Zero or One
?

Matches 0 or 1 of the preceding token (optional)

typescript
/colou?r/.test('color') // true
Zero or One (Lazy)
??

Matches 0 or 1 of the preceding token (lazy)

typescript
'a'.match(/a??/) // ['']
Exact Count
{n}

Matches exactly n occurrences of the preceding token

typescript
/\d{4}/.test('2024') // true
At Least N
{n,}

Matches n or more occurrences of the preceding token

typescript
'aaaa'.match(/a{2,}/) // ['aaaa']
At Least N (Lazy)
{n,}?

Matches n or more occurrences (lazy)

typescript
'aaaa'.match(/a{2,}?/) // ['aa']
Range
{n,m}

Matches between n and m occurrences (greedy)

typescript
'aaaaa'.match(/a{2,4}/) // ['aaaa']
Range (Lazy)
{n,m}?

Matches between n and m occurrences (lazy)

typescript
'aaaaa'.match(/a{2,4}?/) // ['aa']

Groups and References

Capturing Group
(pattern)

Groups pattern and captures the match for backreference

typescript
'abc abc'.match(/(abc) \1/) // ['abc abc', 'abc']
Non-Capturing Group
(?:pattern)

Groups pattern without capturing the match

typescript
/(?:ab)+/.test('abab') // true
Named Capturing Group
(?<name>pattern)

Groups pattern with a named reference

typescript
'2024-01-15'.match(/(?<year>\d{4})/).groups.year // '2024'
Backreference (Numbered)
\n

References the nth captured group

typescript
/(\w+) \1/.test('hello hello') // true
Backreference (Named)
\k<name>

References a named captured group

typescript
/(?<word>\w+) \k<word>/.test('test test') // true
Positive Lookahead
(?=pattern)

Matches if followed by pattern without consuming it

typescript
'100px'.match(/\d+(?=px)/) // ['100']
Negative Lookahead
(?!pattern)

Matches if NOT followed by pattern

typescript
/\d+(?!px)/.test('100em') // true
Positive Lookbehind
(?<=pattern)

Matches if preceded by pattern without consuming it

typescript
'$100'.match(/(?<=\$)\d+/) // ['100']
Negative Lookbehind
(?<!pattern)

Matches if NOT preceded by pattern

typescript
/(?<!\$)\d+/.test('€100') // true
Alternation
a|b

Matches either a or b

typescript
/cat|dog/.test('I have a dog') // true

Flags

Global
g

Find all matches instead of stopping after first match

typescript
'abab'.match(/a/g) // ['a', 'a']
Case Insensitive
i

Makes the pattern case-insensitive

typescript
/hello/i.test('HELLO') // true
Multiline
m

Makes ^ and $ match start/end of each line

typescript
/^b/m.test('a\nb') // true
Dotall
s

Makes dot match newline characters as well

typescript
/a.b/s.test('a\nb') // true
Unicode
u

Enables full Unicode matching and unicode escapes

typescript
/\p{Emoji}/u.test('😀') // true
Sticky
y

Matches only from lastIndex position in target string

typescript
const re = /a/y; re.lastIndex = 1; re.test('bab') // true
Has Indices
d

Generate indices for substring matches

typescript
'abc'.match(/b/d).indices // [[1, 2]]

String Methods

test()
regex.test(string)

Returns true if pattern matches string

typescript
/\d+/.test('abc123') // true
exec()
regex.exec(string)

Returns match array or null with detailed info

typescript
/(\d+)/.exec('abc123') // ['123', '123', index: 3]
match()
string.match(regex)

Returns array of matches or null

typescript
'a1b2c3'.match(/\d/g) // ['1', '2', '3']
matchAll()
string.matchAll(regex)

Returns iterator of all matches with groups (requires g flag)

typescript
[...'a1b2'.matchAll(/(\d)/g)] // detailed matches
replace()
string.replace(regex, replacement)

Replaces first match (or all with g flag) with replacement

typescript
'hello'.replace(/l/g, 'L') // 'heLLo'
replaceAll()
string.replaceAll(regex, replacement)

Replaces all matches (regex must have g flag)

typescript
'aaa'.replaceAll(/a/g, 'b') // 'bbb'
search()
string.search(regex)

Returns index of first match or -1 if not found

typescript
'hello123'.search(/\d/) // 5
split()
string.split(regex)

Splits string by regex matches into array

typescript
'a1b2c3'.split(/\d/) // ['a', 'b', 'c', '']
Replace with Function
string.replace(regex, fn)

Replace using function that receives match details

typescript
'a1b2'.replace(/\d/g, m => m * 2) // 'a2b4'
Replace with Groups
string.replace(regex, '$1')

Replace using captured group references

typescript
'John Smith'.replace(/(\w+) (\w+)/, '$2, $1') // 'Smith, John'

Related Content