Skip to main content
Ganesh Joshi
Back to Cheatsheets

Regular expressions

Updated 2026-02-08

Common regex patterns and syntax for JavaScript and other languages. Anchors, quantifiers, groups, and character classes.

Anchors

`^` start of string/line, `$` end of string/line. In JS use `m` flag for multiline (^/$ match line boundaries).

Quantifiers

`*` 0 or more, `+` 1 or more, `?` 0 or 1, `{n}` exactly n, `{n,}` n or more, `{n,m}` between n and m. Default greedy; add `?` for lazy (e.g. `*?`).

Character classes

`\d` digit, `\w` word char (letter, digit, underscore), `\s` whitespace. Uppercase = negated: `\D`, `\W`, `\S`. `[a-z]` range, `[^x]` not x.

Groups

`(x)` capturing group, `(?:x)` non-capturing, `(?<name>x)` named group. Backreference: `\1` or `\k<name>`. Alternation: `a|b`.

JavaScript

`new RegExp('pattern', 'gi')` or `/pattern/gi`. Flags: `g` global, `i` ignore case, `m` multiline, `s` dotAll. Methods: `test()`, `exec()`, `match()`, `replace()`, `matchAll()`.

Regular expressions | Cheatsheet | Ganesh Joshi