Regular Expression Tools

Javascript Regex Cheat Sheet

Basic Patterns

Pattern Description
^ Matches the beginning of a string.
$ Matches the end of a string.
. Matches any single character except newline characters.
[...] Matches any character listed between the square brackets.
[^...] Matches any character not listed between the square brackets.
\d Matches any digit, equivalent to [0-9].
\D Matches any non-digit.
\w Matches any word character (alphanumeric plus underscore).
\W Matches any non-word character.
\s Matches any whitespace character.
\S Matches any non-whitespace character.
\b Matches a word boundary.
\B Matches a non-word boundary.

Quantifiers

Pattern Description
* Matches zero or more occurrences of the preceding element.
+ Matches one or more occurrences of the preceding element.
? Matches zero or one occurrence of the preceding element.
{n} Matches exactly n occurrences of the preceding element.
{n,} Matches n or more occurrences of the preceding element.
{n,m} Matches between n and m occurrences of the preceding element.

Groups and Lookaround

Pattern Description
(...) Defines a capturing group.
(?:...) Defines a non-capturing group.
(?=...) Positive lookahead assertion.
(?!...) Negative lookahead assertion.
(?<=...) Positive lookbehind assertion.
(? Negative lookbehind assertion.

Flags

Flag Description
g Global search.
i Case-insensitive search.
m Multi-line search.
u Full Unicode matching.
y Sticky search. Matches from the index indicated by the lastIndex property.
s Allows . to match newline characters.

Escape Sequences

Pattern Description
\ Escapes a special character.
\t Matches a tab character.
\n Matches a newline character.
\r Matches a carriage return character.
\f Matches a form feed character.
\v Matches a vertical tab character.
\cX Matches a control character. E.g., \cM matches a Control-M or Enter character.
\xhh Matches the character with the code hh (two hexadecimal digits).
\uhhhh Matches the Unicode character with the code hhhh (four hexadecimal digits).

Unicode Property Escapes

Pattern Description
\p{Property=Value} Matches a character with a specific Unicode property. For example, \p{Script=Greek} matches a character in the Greek script.
\P{Property=Value} Matches a character not having the specified Unicode property.

Named Capture Groups

Pattern Description
(?<name>...) Defines a named capturing group.
\k<name> Backreference to a named group.

Assertions

Pattern Description
(?=...) Positive lookahead. Matches a group after the main expression without including it in the result.
(?!...) Negative lookahead. Specifies a group that can not match after the main expression.
(?<=...) Positive lookbehind. Matches a group before the main expression without including it in the result.
(?<!...) Negative lookbehind. Specifies a group that can not match before the main expression.

Flags in Inline Mode

Pattern Description
(?i) Case-insensitive mode.
(?m) Multiline mode. Changes the behavior of ^ and $.
(?s) Single line mode. Dot matches newline characters.
(?u) Full Unicode matching mode.
(?x) Extended mode. Whitespace is ignored.

Non-capturing Groups

Pattern Description
(?:...) Groups multiple tokens together without creating a capturing group.

Atomic Groups

Pattern Description
(?>...) Atomic group. Doesn't backtrack once a character is matched.

Character Classes

Pattern Description
[^] Matches any character, including newlines. Useful in multiline mode.

Octal Escapes

Pattern Description
\0 Matches a NULL (U+0000) character. Do not follow with a digit.
\n Where n is an octal number. Matches the character with the specified code.

Constructs and Conditionals

Pattern Description
(?(id/name)yes-pattern|no-pattern) Matches the yes-pattern if the group with the given id or name exists, otherwise matches the no-pattern.

Resetting Match Position

Pattern Description
\K Resets the starting position of the reported match. Characters matched previously are not included in the final matched string.

Match Point Reset

Pattern Description
\G Matches the point where the previous match finished. Useful for piecing together multi-part matches.
Copyright © 2019 - RegExpLib.com