Regex Cheatsheet#
Common Regex Patterns#
-
not:
[^abc]
e.g. any character excepta
,b
, orc
-
preceededBy/startsWith:
(?<=a)b
: find+capture the firstexpr b
that is preceeded byexpr a
i.e. positive lookbehind
JavaScript
/(?<=foo)bar/
"foobar" => captures 'bar'
"fffoobar" => captures 'bar'
"fuubar" => false
- !preceededBy/!startsWith:
(?<!a)b
: find+capture the firstexpr b
that is not preceeded byexpr a
i.e. negative lookbehind
JavaScript
/(?<!not)foo/
"notfoo" => false
"not foo" => captures 'foo'
"foo" => captures 'foo'
"foobaz" => captures 'foo'
- followedBy/endsWith:
a(?=b)
: find+capture the firstexpr a
that is followed byexpr b
i.e. positive lookahead
JavaScript
/foo(?=bar)/
"foobar" => captures 'foo'
"foobaz" => false
"fffoobaz" => false
- !followedBy/!endsWith:
a(?!b)
: find+capture the firstexpr a
that is not followed byexpr b
i.e. negative lookahead
JavaScript
/foo(?!bar)/
"foobar" => false
"foobaz" => captures 'foo'
"fffoobaz" => captures 'foo'
- more misc examples
JavaScript
/(?<!if\s|static\s)constexpr/.match(["if constexpr", "static constexpr", "constexpr"]) => ['constexpr','constexpr', false]
/\/\/(?! zig fmt: on|\/).*/