GithubNPM

Regex Guide

A quick reference guide to common regex characters

Regex Search Modifiers

  • marks an alternative acceptable text

    /(?:sample)|(?:other)/
  • requires text to repeat specified amount of times

    /(?:sample){5}/
  • requires text to repeat at least the specified amount of times

    /(?:sample){5, }/
  • requires text to repeat between the minimum and max range specified

    /(?:sample){5, 8}/
  • Marks text as occuring once or more, using a greedy search

    /(?:sample)+/
  • Marks text as occuring once or more, using a lazy search

    /(?:sample)+?/
  • Marks text as occuring zero or more times, using a greedy search

    /(?:sample)*/
  • Marks text as occuring zero or more times, using a lazy search

    /(?:sample)*?/
  • requires search text to be followed by specified text

    /sample(?=after)/
  • requires search text to NOT be followed by specified text

    /sample(?!after)/
  • (?<=...)

    requires search text to be preceded by specified text

    /(?<=pre)sample/
  • requires search text to NOT be preceded by specified text

    /(?<!pre)sample/
  • marks text as occuring at beginning

    /^(?:sample)/
  • marks text as occuring at end

    /(?:sample)$/
  • marks text as optional

    /(?:sample)?/
  • encapsulates text as a group and captures when matching

    /(sample)/
  • (?:...)non-capture grouping

    encapsulates text as a group without capturing

    /(?:sample)/
  • (?<...>...) ... \k<...>

    uses a named capture group to match each \k<...> to the first instance

    /(?<var>)\d{2}) and \k<var>/
SymbolNameDescriptionExample
|marks an alternative acceptable text(?:sample)|(?:other)
{3}requires text to repeat specified amount of times(?:sample){5}
{3,}requires text to repeat at least the specified amount of times(?:sample){5, }
{3, 6}requires text to repeat between the minimum and max range specified(?:sample){5, 8}
+Marks text as occuring once or more, using a greedy search(?:sample)+
+?Marks text as occuring once or more, using a lazy search(?:sample)+?
*Marks text as occuring zero or more times, using a greedy search(?:sample)*
*?Marks text as occuring zero or more times, using a lazy search(?:sample)*?
(?=...)requires search text to be followed by specified textsample(?=after)
(?!...)requires search text to NOT be followed by specified textsample(?!after)
(?<=...)requires search text to be preceded by specified text(?<=pre)sample
(?<!...)requires search text to NOT be preceded by specified text(?<!pre)sample
^marks text as occuring at beginning^(?:sample)
$marks text as occuring at end(?:sample)$
?marks text as optional(?:sample)?
(...)encapsulates text as a group and captures when matching(sample)
(?:...)non-capture groupingencapsulates text as a group without capturing(?:sample)
(?<...>...) ... \k<...>uses a named capture group to match each \k<...> to the first instance(?<var>)\d{2}) and \k<var>

Special Characters

  • matches any single character

  • matches any single character not listed between [^ and ]

  • matches any single digit

  • \Dnon-digit

    matches any single character that is not a digit

  • matches any single letter

  • \Wnon-letter

    matches any single character that is not a letter

  • \sspace

    matches a space between characters such as " "

  • marks a text as having a boundary, such as a space or start of the text

SymbolNameDescription
.matches any single character
[^...]matches any single character not listed between [^ and ]
\dmatches any single digit
\Dnon-digitmatches any single character that is not a digit
\wmatches any single letter
\Wnon-lettermatches any single character that is not a letter
\sspacematches a space between characters such as " "
\bmarks a text as having a boundary, such as a space or start of the text