Regular Expression (RegEx) Overview

A good online tool to test your regex skills: https://regexr.com/

Syntax:

1. /pattern/flags
2. New RegExp(pattern, flags)

Flags:
g : global match
i : ignore case
m : multi-line
u : unicode
y : sticky?
Characters:
. == single character
\d == digit – any capitalized variant such as \D would match the opposite
\w == numbers and alphabet. \W is the opposite
\s == single space. \S is opposite
\t == tab
\r == carriage return
\n == new line
\v == vertical tab
\f == form-feed
[\b] == back-space?
\0 == NULL character
\cX == matches control character A-Z as X
\xhh == two hex digits
\uhhhh == UTF four digits
\u{hhhh} == when u flag is set, match character with unicode value
\ == treat next char as literal (e.g. \. means just the dot ‘.’)
[a-z] == match any character inside square brackets
[^a-z] == match any that is NOT shown in square brackets
x|y == either x or y
^ == beginning of input
$ == end of input, does not allow any character after
\b == word boundary: Example: /\bm/ matches ‘m’ in “moon” but not ‘m’ in “nimble”
\B == not a word boundary
(x) == capturing groups, remembers the x to be recalled later as $1, $2,…
(?:x) == non-capturing groups, no recall later
\n == allow n number of characters proceeding (e.g. /man\1/ will match ‘mana’ in “management”
x* == 0 or multiple times (greedy)
x+ == at least 1 or multiple times
x? == 0 or 1 time
x{n} == repeated x nth number of times
x{n,} == repeated x nth number of times as minimum, no maximum
x{n,m} == repeated x nth minimum and mth maximum
x*? == matches x multiple times, but non-greedy. Example: /.*?-/ will match ‘714-‘ in ‘714-555-5555’ whereas /.*-/ will match ‘714-555-‘
x(?=y) == x only if followed by y
x(?!y) == x only if NOT followed by y
Usage:
var regex = /(.)\1+/g;
str.match(regex);
Examples:
Match repeating characters in group 1 one more more times: /(.)\1+/
Digit: \d
Word (whole word): \w
Negate (meaning NOT): \D \W \S \ ^[a-zA-Z]
Escape character: \
Literal charaters: example: Kimconnect\.com
Any character (alpha/numeric/symbols): .
Whitespace: \s
Line breaks: \n
Return character: \r
Range: [a-zA-Z0-9]
Ignore case sensitivity: /i example: string.match("Mr.?", "i") to match Mr, mR, MR, with or without dot .
Quantifier:
? : zero or 1 character of preceding value
* : a.*e will match apple and automobile
+ : one or more, usually follows range [a-zA-Z]+
{2} : matches 2 preceding characters
Group: (Mr|Ms|Mrs)\.?(\w)
1st Group (honorific title): $1
2nd Group (name): $2


Leave a Reply

Your email address will not be published. Required fields are marked *