Luna Tech | Regex Reference Tables

December 13, 2019 字数 400 2 min


Related Articles

Luna Tech: Regex Tutorial for Everyone

Note: For a better reading experience, please open this link in a computer browser, the mobile view really messed up the formatting, especially the tables.

1. Metacharacters

Metacharacter Meaning
/ The start/end of the regex
e.g., /pattern/
\ Escape from special meaning
e.g., /0\.9/
. Wildcard metacharacter, matches any character except newline
e.g., /0.9/
* item before appear 0 or more times
e.g., /a*/
+ item before appear 1 or more times
e.g., /a+/
? item before appear 0 or 1 time
e.g., /a?/
- Defines a character range
e.g., /[0-9]/
pipe Alternation (OR)
{ } Quantified Repetition
e.g., /a{3,5}/
[ ] Defines a Character Set
e.g., /[aeiou]/
( ) Defines a group
e.g., /(group1)-(group2)/
^ 1. When used at the start of a character set, defines a negative set.
e.g., /[^aeiou]/
2. When used at the start of Regex, means the start of string/line.
e.g., /^Banana/
$ End of string/line
e.g., /Banana$/
\A Start of string, not end of line
e.g., /\ABanana/
JS engine not support
\Z End of string, not end of line
e.g., /Banana\Z/
JS engine not support
\b Word boundary (start/end of a word)
e.g., /\bApple\b/
\B Not a word boundary
e.g., /\Bea\B/

Note:

| cannot be correctly rendered in the markdown table above, so I use pipe for alternation.

e.g., /match1|match2|match3)/


2. Special Characters

Special Character Meaning
\t Tab
\r Newline, CR (Short for Carriage Return)
It just means to end the current line, not to start a new line.
\n Newline, LF (Short for Line Feed)
Unix and Linux use this to represent an enter keypress
\r\n Newline, CRLF (Short for Carriage Return + Line Feed)
Windows uses this to represent an enter keypress

3. Character ranges

Character ranges Explanation
[0-9] Match any number between 0 and 9
[A-Z] Match any uppercase characters from A to Z
[a-z] Match any lowercase characters from a to z
[A-Za-z] Match any character from a to z, case insensitive
[a-f] Match any lowercase character from a to f
This is an example of how you can use the range to customize your needs.

4. Shorthand character sets

Shorthand Character Set Explanation
\d [0-9] Digit
\w [a-zA-Z0-9_] Word Character
(include _ but exclude -)
\s [ \t\r\n] Whitespace
\D [^0-9] Not a digit
\W [^a-zA-Z0-9_] Not a word character
(exclude _ but include -)
\S [^ \t\r\n] Not a whitespace

Talk to Luna


Support Luna