Regex-fusklapp
Character classes
| Syntax | Beskrivning | Exempel | Kopiera |
|---|---|---|---|
| . | Any character except newline | a.c → abc, a-c | |
| [abc] | Match a, b, or c | [xyz] → x, y, z | |
| [^abc] | Match anything except a, b, c | ||
| [a-z] | Character range (a to z) | ||
| \d | Digit (0-9) | \d{3} → 123 | |
| \D | Non-digit | ||
| \w | Word character [a-zA-Z0-9_] | ||
| \W | Non-word character | ||
| \s | Whitespace | ||
| \S | Non-whitespace |
Anchors
| Syntax | Beskrivning | Exempel | Kopiera |
|---|---|---|---|
| ^ | Start of string/line | ^Hello | |
| $ | End of string/line | World$ | |
| \b | Word boundary | \bword\b | |
| \B | Non-word boundary |
Escaped characters
| Syntax | Beskrivning | Exempel | Kopiera |
|---|---|---|---|
| \t | Tab | ||
| \n | Newline | ||
| \r | Carriage return | ||
| \0 | Null character | ||
| \xhh | Hex character (e.g. \xFF) | ||
| \uhhhh | Unicode character |
Groups & References
| Syntax | Beskrivning | Exempel | Kopiera |
|---|---|---|---|
| (abc) | Capturing group | ||
| (?:abc) | Non-capturing group | ||
| (?<name>abc) | Named capturing group | ||
| \1 | Back-reference to group 1 | ||
| \k<name> | Back-reference to named group |
Lookaround
| Syntax | Beskrivning | Exempel | Kopiera |
|---|---|---|---|
| (?=abc) | Positive lookahead | foo(?=bar) matches foo in foobar | |
| (?!abc) | Negative lookahead | ||
| (?<=abc) | Positive lookbehind | ||
| (?<!abc) | Negative lookbehind |
Quantifiers & Alternation
| Syntax | Beskrivning | Exempel | Kopiera |
|---|---|---|---|
| a* | 0 or more (greedy) | ||
| a+ | 1 or more (greedy) | ||
| a? | 0 or 1 (optional) | ||
| a{n} | Exactly n times | ||
| a{n,} | n or more times | ||
| a{n,m} | Between n and m times | ||
| a*? | 0 or more (lazy) | ||
| a+? | 1 or more (lazy) | ||
| a|b | Match a or b |
Substitution
| Syntax | Beskrivning | Exempel | Kopiera |
|---|---|---|---|
| $1, $2 | Captured group content | ||
| $& | Entire match | ||
| $` | Before match | ||
| $' | After match | ||
| $$ | Literal $ character |
Flags
| Syntax | Beskrivning | Exempel | Kopiera |
|---|---|---|---|
| g | Global — match all occurrences | ||
| i | Case-insensitive | ||
| m | Multiline — ^ and $ match line breaks | ||
| s | Dotall — . matches newlines too | ||
| u | Unicode — full Unicode support | ||
| y | Sticky — match at lastIndex only |