Most characters match themselves. The special ones below build classes, anchors, quantifiers and lookarounds; put a backslash before any of . \ + * ? [ ] ^ $ ( ) { } | / to match it literally.
| Syntax | Matches | Syntax | Matches |
|---|---|---|---|
| . | any character but a line break | \d \w \s | digit, [A-Za-z0-9_], whitespace |
| [abc] [^abc] [a-z] | one of, none of, a range | \D \W \S | the opposites |
| a|b | a or b | \b \B | word boundary / not one |
| ^ $ | start / end of input (line with m) | \p{…} \P{…} | has / lacks a Unicode property |
| * + ? {n,m} | 0+, 1+, 0-1, n to m times | \n \t \xhh \u{h…} | control and coded characters |
| *? +? {n,m}? | lazy: as few as possible | (?=x) (?!x) (?<=x) (?<!x) | followed / preceded (or not) by x |
Quantifiers are greedy; a trailing ? makes them lazy. Lookarounds test context without including it in the match.
const s = 'a-bb-ccc-dddd';
console.log(s.replace(/[^-]{3,}/g, 'x'), s.replace(/[^-]{3,}?/g, 'x')); // greedy vs lazy
console.log('100 USD, 250 EUR'.match(/\d+(?= EUR)/)[0], 'is $100'.match(/(?<=\$)\d+/)[0]);
const sentence = 'A ticket to 大阪 costs ¥2000 👌.';
console.log(sentence.match(/\p{Emoji_Presentation}|\p{Sc}|\p{Script=Han}+/gu));a-bb-x-x a-bb-x-xd 250 100 [ '大阪', '¥', '👌' ]
\p{…} needs the u or v flag. Beware [A-z]: besides letters it spans [ \ ] ^ _ and the backtick.