Regular expressions
A regular expression is a tiny pattern language for describing text: "two digits, a dot, then four digits". Type a pattern below and watch it match, capture and replace live.
Live tester
Matches update as you type. Invalid patterns are explained instead of crashing.
//
One or more name characters, an @, a domain, then a dot and the top-level domain.
Flags
Result — 2 matches
Contact ada@example.com or grace.hopper+navy@navy.mil, not 'nope@'.
Matches & capture groups
| # | Index | Match | Groups |
|---|---|---|---|
| 1 | 8 | ada@example.com | — |
| 2 | 27 | grace.hopper+navy@navy.mil | — |
Use $1, $2 for numbered groups, $<name> for named ones and $& for the whole match.
Contact [email] or [email], not 'nope@'.
Live examples
Load one into the tester and tweak it.
Cheat sheet
The pieces you will use ninety percent of the time.
Characters
- .
- Any character except newline
- \d \D
- Digit / not a digit
- \w \W
- Word character [A-Za-z0-9_] / not
- \s \S
- Whitespace / not whitespace
- [abc]
- Any one of a, b, c
- [^abc]
- Anything except a, b, c
- [a-z0-9]
- Ranges inside a character class
- \. \+ \\
- Escape a special character
Quantifiers
- *
- 0 or more
- +
- 1 or more
- ?
- 0 or 1 (optional)
- {3}
- Exactly 3
- {2,5}
- Between 2 and 5
- {2,}
- 2 or more
- +? *?
- Lazy — match as little as possible
Anchors & groups
- ^ $
- Start / end of string (or line with m)
- \b \B
- Word boundary / not a boundary
- (abc)
- Capturing group
- (?:abc)
- Group without capturing
- (?<name>abc)
- Named capturing group
- a|b
- Either a or b
- \1
- Backreference to group 1
- (?=x) (?!x)
- Lookahead: followed / not followed by x
- (?<=x) (?<!x)
- Lookbehind: preceded / not preceded by x