Regular Expressions Cheatsheet
regexRegex syntax reference for JavaScript developers.
Character Classes
[abc]Matches any single character in the brackets
/[aeiou]/.test('hello') // true[^abc]Matches any single character NOT in the brackets
/[^0-9]/.test('abc') // true[a-z]Matches any character in the specified range
/[A-Za-z]/.test('Hello') // true.Matches any single character except newline (unless s flag)
/h.t/.test('hat') // true\wMatches alphanumeric character plus underscore [A-Za-z0-9_]
'hello_123'.match(/\w+/) // ['hello_123']\WMatches any non-word character [^A-Za-z0-9_]
'hello world'.match(/\W/) // [' ']\dMatches any digit [0-9]
'abc123'.match(/\d+/) // ['123']\DMatches any non-digit character [^0-9]
'abc123'.match(/\D+/) // ['abc']\sMatches any whitespace character (space, tab, newline)
'a b'.match(/\s/) // [' ']\SMatches any non-whitespace character
' hello '.match(/\S+/) // ['hello']Anchors
^Matches the start of string (or line with m flag)
/^hello/.test('hello world') // true$Matches the end of string (or line with m flag)
/world$/.test('hello world') // true\bMatches a word boundary position
/\bcat\b/.test('the cat sat') // true\BMatches a non-word boundary position
/\Bcat/.test('concat') // trueQuantifiers
*Matches 0 or more of the preceding token (greedy)
'aaa'.match(/a*/) // ['aaa']*?Matches 0 or more of the preceding token (lazy)
'aaa'.match(/a*?/) // ['']+Matches 1 or more of the preceding token (greedy)
'aaa'.match(/a+/) // ['aaa']+?Matches 1 or more of the preceding token (lazy)
'aaa'.match(/a+?/) // ['a']?Matches 0 or 1 of the preceding token (optional)
/colou?r/.test('color') // true??Matches 0 or 1 of the preceding token (lazy)
'a'.match(/a??/) // ['']{n}Matches exactly n occurrences of the preceding token
/\d{4}/.test('2024') // true{n,}Matches n or more occurrences of the preceding token
'aaaa'.match(/a{2,}/) // ['aaaa']{n,}?Matches n or more occurrences (lazy)
'aaaa'.match(/a{2,}?/) // ['aa']{n,m}Matches between n and m occurrences (greedy)
'aaaaa'.match(/a{2,4}/) // ['aaaa']{n,m}?Matches between n and m occurrences (lazy)
'aaaaa'.match(/a{2,4}?/) // ['aa']Groups and References
(pattern)Groups pattern and captures the match for backreference
'abc abc'.match(/(abc) \1/) // ['abc abc', 'abc'](?:pattern)Groups pattern without capturing the match
/(?:ab)+/.test('abab') // true(?<name>pattern)Groups pattern with a named reference
'2024-01-15'.match(/(?<year>\d{4})/).groups.year // '2024'\nReferences the nth captured group
/(\w+) \1/.test('hello hello') // true\k<name>References a named captured group
/(?<word>\w+) \k<word>/.test('test test') // true(?=pattern)Matches if followed by pattern without consuming it
'100px'.match(/\d+(?=px)/) // ['100'](?!pattern)Matches if NOT followed by pattern
/\d+(?!px)/.test('100em') // true(?<=pattern)Matches if preceded by pattern without consuming it
'$100'.match(/(?<=\$)\d+/) // ['100'](?<!pattern)Matches if NOT preceded by pattern
/(?<!\$)\d+/.test('€100') // truea|bMatches either a or b
/cat|dog/.test('I have a dog') // trueFlags
gFind all matches instead of stopping after first match
'abab'.match(/a/g) // ['a', 'a']iMakes the pattern case-insensitive
/hello/i.test('HELLO') // truemMakes ^ and $ match start/end of each line
/^b/m.test('a\nb') // truesMakes dot match newline characters as well
/a.b/s.test('a\nb') // trueuEnables full Unicode matching and unicode escapes
/\p{Emoji}/u.test('😀') // trueyMatches only from lastIndex position in target string
const re = /a/y; re.lastIndex = 1; re.test('bab') // truedGenerate indices for substring matches
'abc'.match(/b/d).indices // [[1, 2]]String Methods
regex.test(string)Returns true if pattern matches string
/\d+/.test('abc123') // trueregex.exec(string)Returns match array or null with detailed info
/(\d+)/.exec('abc123') // ['123', '123', index: 3]string.match(regex)Returns array of matches or null
'a1b2c3'.match(/\d/g) // ['1', '2', '3']string.matchAll(regex)Returns iterator of all matches with groups (requires g flag)
[...'a1b2'.matchAll(/(\d)/g)] // detailed matchesstring.replace(regex, replacement)Replaces first match (or all with g flag) with replacement
'hello'.replace(/l/g, 'L') // 'heLLo'string.replaceAll(regex, replacement)Replaces all matches (regex must have g flag)
'aaa'.replaceAll(/a/g, 'b') // 'bbb'string.search(regex)Returns index of first match or -1 if not found
'hello123'.search(/\d/) // 5string.split(regex)Splits string by regex matches into array
'a1b2c3'.split(/\d/) // ['a', 'b', 'c', '']string.replace(regex, fn)Replace using function that receives match details
'a1b2'.replace(/\d/g, m => m * 2) // 'a2b4'string.replace(regex, '$1')Replace using captured group references
'John Smith'.replace(/(\w+) (\w+)/, '$2, $1') // 'Smith, John'