Regex Cheatsheet#
Common Regex Patterns#
-
not:
[^abc]e.g. any character excepta,b, orc -
preceededBy/startsWith:
(?<=a)b: find+capture the firstexpr bthat is preceeded byexpr ai.e. positive lookbehind
JavaScript
/(?<=foo)bar/
"foobar" => captures 'bar'
"fffoobar" => captures 'bar'
"fuubar" => false
- !preceededBy/!startsWith:
(?<!a)b: find+capture the firstexpr bthat is not preceeded byexpr ai.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 athat is followed byexpr bi.e. positive lookahead
JavaScript
/foo(?=bar)/
"foobar" => captures 'foo'
"foobaz" => false
"fffoobaz" => false
- !followedBy/!endsWith:
a(?!b): find+capture the firstexpr athat is not followed byexpr bi.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|\/).*/