Quick reference

Basics

PomskyExplanationRegex
"string"Stringstring
'string'Raw string (no backslash escaping)string
'a' | 'b'a OR ba|b
('a' | 'b')Group (non-capturing)(?:a|b)
# commentLine comment(?#comment)
.any code point except \n 1.

Repetitions

PomskyExplanationRegex
'test'*0 or more(?:test)*
'test'+1 or more(?:test)+
'test'?0 or 1(?:test)?
'test'{4,}4 or more times(?:test){4,}
'test'{4,7}4 to 7 times(?:test){4,7}
'test'+ lazyLazy (non-greedy) repetition(?:test)+?

Variables

let x = 'foo' | 'bar';

x '-' x

Variables are replaced with their content in the produced regex:

(?:foo|bar)-(?:foo|bar)

Character sets

PomskyExplanationRegex
[···]character set[···]
![···]negated character set[^···]
[n t]special characters (line feed, tab)[\n\t]
['a' 'd']an ‘a’ or ’d’[ad]
['ad']an ‘a’ or ’d’[ad]
['a'-'d']code points ‘a’ through ’d’[a-d]
[U+45 U+FFEF]code points U+0045 and U+FFEF[\x45\uFFEF]

Built-in character classes

PomskyExplanationRegex
[word], [w]any ‘word’ code point (letter, digit, or _)\w
[Latin]any code point in the ‘Latin’ script\p{Latin}
[Letter]any code point in the ‘Letter’ category\p{Letter}
[ascii_digit]any ASCII digit[0-9]
[!word]any code point except ‘word’ code points\W

See all shorthand character classes and all supported Unicode properties.

Anchors, boundaries, assertions

These don’t match one or more characters, but a position in the string.

PomskyExplanationRegex
^start of string 2^
$end of string 2$
%word boundary\b
!%not a word boundary\B
<start of a word
>end of a word
(>> 'x')lookahead assertion(?=x)
(<< 'x')lookbehind assertion(?<=x)
(!>> 'x')negated lookahead assertion(?!x)
(!<< 'x')negated lookbehind assertion(?<!x)

Capturing groups and references

PomskyExplanationRegex
:('foo')capturing group(foo)
:bar('foo')capturing group named ‘bar’(?<bar>foo)
::1reference to 1st capturing group\1
::barreference to group named ‘bar’\k<bar>
::-2relative backreference\k<-2>
::+1relative forward reference

Wildcard patterns

PomskyExplanationRegex
.any code point except \n 1.
Codepoint, Cany code point[\s\S]
Grapheme, Gany grapheme\X

Testing

test {
  match 'foo';

  match 'the', 'fox', 'the', 'dog'
     in 'the quick brown fox jumps over the lazy dog.';

  reject 'lazy';

  reject in 'lorem ipsum dolor';
}

% [w]{3} %

Comparing capturing groups:

test {
  match '13.1.4' as { major: '13', minor: '1', patch: '4' };

  match '13.1.4' as { 1: '13', 2: '1', 3: '4' };
}

:major([d]+) '.' :minor([d]+) '.' :patch([d]+)

Modifiers

PomskyExplanationRegex
enable lazy;enable lazy repetition by default(?U)
disable lazy;disable lazy repetition by default
enable unicode;enable Unicode awareness (enabled by default)
disable unicode;disable Unicode awareness

Other

PomskyExplanationRegex
range '0'-'255'all decimal numbers from 0 to 255
range '0'-'1FF' base 16all hex numbers from 0 to 1FF
atomic('foo')atomic group(?>foo)
recursionrecursively match the whole regex\g<0>
regex '[]acf-X]'inline regex[]acf-X]

Footnotes


  1. with the single-line flag, . also matches line breaks. ↩︎ ↩︎

  2. in multiline mode, these match the start or end of the line. ↩︎ ↩︎