Regex Tester
Enter a regular expression pattern and a test string to highlight matches and list capture group values. Supports the g/i/m/s/u flags using JavaScript's native RegExp.
Common regex patterns
| Use case | Pattern | Description |
|---|---|---|
| Email address | ^[\w.+-]+@[\w-]+\.[\w.-]+$ | Matches a simplified email address format with a username and domain separated by @ (not fully RFC-compliant). |
| US phone number | ^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$ | Matches common US phone number formats, with or without parentheses and separators (e.g. (555) 123-4567, 555-123-4567). |
| URL | https?:\/\/[\w.-]+(?:\/[\w\-./?%&=]*)? | Extracts URL strings starting with http:// or https://, including any path and query parameters. |
| US ZIP code | ^\d{5}(-\d{4})?$ | Matches a 5-digit ZIP code, optionally followed by the 4-digit ZIP+4 extension. |
| Hex color code | ^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$ | Matches CSS hex colors in the full #RRGGBB form or the shorthand #RGB form. |
| Digits only | ^[0-9]+$ | Checks whether the entire string consists only of digits (0-9). |
Tips
- The
/flagsshown next to the pattern field lets you see at a glance which flag combination is currently active. - Only the parts wrapped in
()count as capture groups. Use a non-capturing group(?:...)if you just need to group a condition without cluttering up the groups list. - Without the g flag, only the first match is found. Turn on the g flag if you want to count every occurrence in the text.
- The "Insert sample" button loads an email-extraction example, which is a good starting point for understanding how capture groups behave.
- All matching happens entirely inside your browser — the pattern and test string you type are never sent to any server.
Frequently Asked Questions
[abc], \d, +, *, etc.) is largely shared, but named group syntax, lookahead/lookbehind support, and available flags differ in subtle ways between languages. This tool follows JavaScript's RegExp specification, so it doesn't guarantee identical behavior elsewhere.*, +, ?) with nothing before it, or an invalid character class. The error message usually gives a hint, so check your parenthesis balance and escaping of special characters (like \. or \().
Side Note — What happens inside a regex engine
Regular expressions trace back to the theory of "regular languages" proposed by mathematician Stephen Kleene in the 1950s. Early Unix tools such as the ed text editor and the grep command (short for "globally search a Regular Expression and Print") adopted this theory as a practical implementation, which is how the term spread rapidly through text-processing tools. It is telling that the name "grep" itself derives from a regex operation.
JavaScript's RegExp engine matches patterns internally using a strategy called backtracking: it tries each element of the pattern in order and, if a later part fails, steps back and tries an alternative. This flexibility comes with a downside — patterns like (a+)+b combined with a long non-matching string can cause the number of attempts to grow exponentially, a phenomenon known as "catastrophic backtracking" that can freeze a browser or server. Always test complex patterns against a variety of inputs before shipping them to production.
A regex object with the global (g) flag keeps an internal cursor called lastIndex, and each call to exec() resumes searching from that position. Reusing the same regex object across unrelated searches can leave this cursor in an unexpected state, so this tool creates a brand-new RegExp instance every time to avoid carrying over any stale state.
Named capture groups ((?<name>...)) were added in ECMAScript 2018 and are a relatively recent feature. Referring to a group by name instead of by number makes complex patterns far more readable — especially useful in situations like date parsing, where knowing what each group represents really matters.