Regex Syntax
/pattern/flags | Literal regex |
new RegExp('pattern', 'flags') | Dynamic regex |
^start / end$ | Anchors |
. (any) \d (digit) \w (word) \s (space) | Character classes |
[abc] [^abc] [a-z] | Character sets |
? * + {n} {n,m} | Quantifiers |
(group) (?:non-capture) (? | Groups |
(?=ahead) (?!neg-ahead) (?<=behind) | Lookaheads/behinds |
Flags
g — global | Find all matches |
i — case-insensitive | Ignore case |
m — multiline | ^ and $ match line boundaries |
s — dotAll | . matches newlines |
u — unicode | Full unicode support |
Methods
str.match(re) | Find matches |
str.matchAll(re) | Iterator of all matches (g flag) |
str.replace(re, repl) | Replace |
str.replaceAll(re, repl) | Replace all |
re.test(str) | Returns boolean |
re.exec(str) | Returns match array |