Skip to content

Instantly share code, notes, and snippets.

@coolaj86
Last active November 13, 2024 18:12
Show Gist options
  • Save coolaj86/129f2506e042828b7bcf0f6cc9735d4b to your computer and use it in GitHub Desktop.
Save coolaj86/129f2506e042828b7bcf0f6cc9735d4b to your computer and use it in GitHub Desktop.
\       escape

^       match start (or negate group)
$       match end

.       match any character (except newlines, [\s\S] to match newlines too)
[]      match range (see examples below)
[^]     negative match range, such as [^<]* (match up to but not including the next `<`)

\w, \W  match any word character [a-zA-Z0-9_], NON-word character [^a-zA-Z0-9_]
\d, \W match any digit [0-9], NON-digit [^0-9]
\s, \S match any space [\t\v ], NON-space [^\t\v ] (also nbsp, etc, and \r\n in multiline mode... maybe)

\b    match a word boundary (without capturing it), including start and end of line

+      match some (1+), such as \d+ (1 or more digits)
*      match any (0+), such as .* (match all of anything)
?      optional match (0 or 1)
{n,m}  from n to m matches, such as \d{9,10} (match 9 or 10 digits - a phone number)

()     capture group, such as (prefix)?(middle)(suffix)? matches prefixmiddle, middle, middlesuffix, prefixmiddlesuffix
|      OR, such as (foo|bar)baz matches foobaz and barbaz
g      find and replace all
m      span multiple lines
/(foo)?(bar)(baz)?/gm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment