By ToolzHive · Updated
These examples use JavaScript regex. Other engines differ. Test positive and negative cases, and avoid unbounded patterns on untrusted long input.
Reference table
| Token | Meaning | Example |
|---|---|---|
| \d | Digit 0–9 | \d{4} |
| \w | ASCII word character, with case-folding exceptions | \w+ |
| \s | Whitespace | \s+ |
| . | Any character except line terminators unless s is enabled | a.b |
| ^ / $ | Start / end assertions | ^cat$ |
| [abc] | One character in a set | [aeiou] |
| [^abc] | One character outside a set | [^0-9] |
| * / + / ? | Zero or more / one or more / optional | colou?r |
| {n,m} | Bounded repetition | \d{2,4} |
| (…) | Capturing group | (red|blue) |
| (?:…) | Non-capturing group | (?:red|blue) |
| \b | Word boundary | \bcat\b |
| g / i | Global / ignore case | /cat/gi |
| m / s | Line assertions / dot matches line terminators | /a.b/s |
| u | Unicode-aware mode | /./u |
Copyable example
const pattern = /^[A-Z]{2}-[0-9]{4}$/;
pattern.test("AB-1234"); // true Worked example
^[A-Z]{2}-[0-9]{4}$ matches AB-1234. Test lowercase letters, spaces and missing digits. JavaScript $ can also match before a final line terminator.
Escaping
A regex literal /\d+/ and a RegExp string need different escaping: new RegExp("\\d+"). A short email pattern does not provide complete address validation.
Try the related tools
- Regex Tester — Test regular expressions with live match highlighting and groups.