Regex Tester
Test and highlight regex matches, replace text. Supports flags g, i, m, u, s. Copy results. No duplicate flags.
A regular expression (regex) is a pattern that describes text. For example, “one or more digits”, “word boundary”, or “optional prefix”. Use this tool to test a regex against your own text: enter your pattern and optional flags (g, i, m, u, s), then click Test to see matches and highlights, or Replace All to replace matches with your replacement string. Flags are normalized (no duplicate characters). For very long text, consider limiting input to avoid slow backtracking.
Loading…
Examples
- Pattern (abc)/g on "abc abc" → 2 matches
- Replace \s+ with space
- Flags: gi for case-insensitive global match
FAQ
What is a regex?
A regular expression (regex) is a sequence of characters that defines a search pattern. It is used to find, validate, or replace text—for example, to match email addresses, numbers, or specific words. Patterns use special characters (e.g. *, +, ?, \d, []) to mean "zero or more", "one or more", "optional", "digit", or "character class". This tool lets you type a regex and test it against your own text.
What regex flags are supported?
g (global), i (ignore case), m (multiline), u (unicode), s (dotall). Enter them in the Flags field. Duplicates are ignored.
Why do I get 'Invalid flags'?
Only g, i, m, s, u, y are allowed. If you see duplicate flags (e.g. gg), the tool normalizes them automatically.
How do I replace all matches?
Enter your pattern and flags, type the replacement text in the Replacement field, then click Replace All. Use $1, $2 for capture groups.
What are the most common regex character classes?
\d matches a digit (0-9). \w matches a word character (a-z, A-Z, 0-9, _). \s matches whitespace (space, tab, newline). \D, \W, \S match the opposite. [abc] matches a, b, or c. [^abc] matches anything except a, b, c.
What are anchors in regex?
^ matches the start of a string (or line with the m flag). $ matches the end. \b matches a word boundary (between \w and \W). Example: ^hello$ matches only the string 'hello' and nothing else.
What are quantifiers?
* matches 0 or more times. + matches 1 or more. ? matches 0 or 1 (makes the preceding element optional). {n} matches exactly n times. {n,m} matches between n and m times. {n,} matches n or more.
What is the difference between greedy and lazy matching?
Greedy quantifiers (*, +, {n,}) match as much as possible. Lazy quantifiers (*?, +?, {n,}?) match as little as possible. Example: on '', <.*> matches the whole string (greedy), while <.*?> matches '' then '' (lazy).
What are capture groups and how do I reference them?
Parentheses create capture groups: (abc). Group 1 is $1 in replacements and \1 in patterns. (?:abc) is a non-capturing group (useful for grouping without creating a reference). Named groups: (?
What is a lookahead and lookbehind?
A lookahead (?=pattern) asserts that what follows matches pattern without consuming it. A negative lookahead (?!pattern) asserts it does not match. Lookbehind (?<=pattern) checks what precedes. Example: \d+(?= dollars) matches numbers followed by ' dollars'.
What is the difference between test() and match() in JavaScript?
regex.test(str) returns true/false — use it when you only need to know if there is a match. str.match(regex) returns an array of matches (or null). With the g flag, match() returns all matches; without it, it returns the first match with capture groups.
What are common regex patterns for validation?
Email (basic): ^[\w.-]+@[\w.-]+\.\w{2,}$. URL: ^https?://[\S]+$. Phone (US): ^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$. Zip code (US): ^\d{5}(-\d{4})?$. These are simplified — real validation often requires more nuance.
What causes catastrophic backtracking?
Catastrophic backtracking occurs when a regex engine tries exponentially many combinations on a non-matching string. Example: (a+)+ on 'aaaaab'. Avoid nested quantifiers on overlapping patterns. Use atomic groups or possessive quantifiers where supported.
What does the dotall (s) flag do?
By default, the dot (.) matches any character except a newline. With the s (dotall) flag, dot also matches newlines. This is useful for matching HTML tags or multi-line strings that span line boundaries.