Snippets are tiny notes I've collected for easy reference.
Cheat Sheet for JavaScript Regular Expressions
flags
/pattern/g
- global/pattern/i
- case-insensitive/pattern/m
- multi-line
patterns
\s
- any whitespace character ([\f\n\r\t\v\u00A0\u2028\u2029]
)\S
- any non-whitespace character ([^\f\n\r\t\v\u00A0\u2028\u2029]
)[\s\S]
- commonly used for "anything including newlines (alternative[^]
)\S
- any non-whitespace character ([^\f\n\r\t\v\u00A0\u2028\u2029]
)\w
- any word character (alpha, numeric or underscore) ([a-zA-Z0-9_]
)\W
- any non-word character ([^a-zA-Z0-9_]
)\d
- any digit ([0-9]
)\D
- any non-digit ([^0-9]
)\cX
- control character X (e.g.\cM
matchescontrol-M
(^M
))\b
- word boundary (the position between a word char and whitespace)\B
- not a word boundary ([^\b]
).\xhh
- the character with hex codehh
\uhhhh
- the character with hex codehhhh
Published 18 Jan 2013
Snippets are tiny notes I've collected for easy reference.