Skip to content

Instantly share code, notes, and snippets.

@regexyl
Last active February 25, 2022 13:43
Show Gist options
  • Select an option

  • Save regexyl/f465d8362c2b7c77284b1455b1f8c5ed to your computer and use it in GitHub Desktop.

Select an option

Save regexyl/f465d8362c2b7c77284b1455b1f8c5ed to your computer and use it in GitHub Desktop.

Regex Cheatsheet

The JavaScript version.

Frequent Examples

Syntax

  • Metacharacters
    • .: Any one character except newline, same as [^\n].
    • \d, \D: Any one digit/non-digit character (where digits are [0-9]).
    • \w, \W: Any one word/non-word character. For ASCII, word characters are [a-zA-Z0-9_].
    • \s, \S: Any one space/non-space character. For ASCII, whitespace characters are [ \n\r\t\f].
  • Occurrence Indicators
  • +: One or more, e.g. [0-9]+ matches 1 or more digits, such as "123", "0000".
    • *: Zero or more (accepts the above + empty strings).
    • ?: Zero or one (optional), e.g., [+-]? matches an optional "+", "-", or an empty string.
    • {}
      • {m,n}: m to n (both inclusive).
      • {m}: Exactly m times.
      • {m,}: m or more times (m+).
  • Position Anchors
    • ^: Start of line, e.g. ^[0-9]$ matches a numeric string.
    • $: End of line
    • \b: Boundary of word, i.e., start-of-word or end-of-word. E.g., \bcat\b matches the word "cat" in the input string.
    • \B: Inverse of \b, i.e. non-start-of-word or non-end-of-word.
  • Parenthesized Back References
    • ():
  • Character Class (or Bracket List)
    • []
    • [...]: Accept any one of the character within the bracket.
    • [.-.]: Accept any one of the characters in the range, e.g. [0-9], [A-Za-z].
    • [^...]: Rejects any one of the character, e.g. [^0-9] matches any non-digit.
    • Only ^, -, ], \ require escape sequence inside the bracket list.
  • |: OR operator, e.g. four|4 accepts "four" or "4".
  • \: Escape sequence to accept a char with special meaning in regex.
    • Regex recognizes common escape sequences such as \n for newline, \t for tab, \r for carriage-return, \nnn for a up to 3-digit octal number, \xhh for a two-digit hex code, \uhhhh for a 4-digit Unicode, \uhhhhhhhh for a 8-digit Unicode.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment