Skip to content

Instantly share code, notes, and snippets.

@jamestalmage
Last active November 22, 2015 08:00
Show Gist options
  • Save jamestalmage/ef979eed50b0f5a23062 to your computer and use it in GitHub Desktop.
Save jamestalmage/ef979eed50b0f5a23062 to your computer and use it in GitHub Desktop.
/pattern/flags
new Regexp(pattern[, flags])

flags: g global, i ignore case, m multiline

regex.test(string) or string.search(regex) - returns true or false

regex.exec(string) - null or an array. 0 index is matched string, 1+ is captures. Maintains state for continuation searches /g flag is set.

Special Chars

char desc
. not newline [^\n\r\u2028\u2029]
\d digits [0-9]
\w alphanumeric and underscore [A-Za-z0-9_]
\s white space [ \f\n\r\t\v​] (oodles of unicode characters not shown)
\t tab
\r carriage return
\n line feed
\v vertical tab
\f form feed
[\b] backspace (not to be confused with \b)
\0 NUL char, do not follow with another digit
\cX where X is a letter A-Z. Matches a control character (/\cM/ matches control-M)
\xhh matches char with code hh (2 hex digits).
\uhhhh matches char with Unicode value hhhh (four hex digits)

Boundaries

char desc
^ beginning of input. (also start of line if multiline flag is set)
$ end of input. (also end of line if multiline flag is set)
\b zero-width word boundry. /\bno ==> "nono nono"
\B zero-width non-word boundry. (i.e. between 2 letters or between 2 spaces)

Grouping

char desc
(x) Match and remember (matches start at index 1 in the array)
\n where n is a positive integer. A back reference. See below.
(?:x) Match but do not remember

Quatifiers

char desc
x* match x zero or more times
x+ one or more times. equivalent to x{1,}
x? zero or one times.
x(?=y) match x only if followed by y
x(?!y) match x only if not followed by y
x|y matches x or y
x{n} x exactly n times
x{n,} x at least n times
x{n,m} x between n and m times
? Added to any of the quantifiers *, +, ?, or {} makes it non-greedy (everything is greedy by default)

Character Sets

char desc
[xyz] match any character in the list (use hyphens for ranges)
[^xyz] match any character NOT in the list (use hyphens for ranges)

Detailed Back Reference Explanation (excerpt)

Backreferences match the same text as previously matched by a capturing group (i.e. ()). To figure out the number of a particular backreference, scan the regular expression from left to right. Count the opening parentheses of all the numbered capturing groups. The first parenthesis starts backreference number one, the second number two, etc. Skip parentheses that are part of other syntax such as non-capturing groups.

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