How to Test Regular Expressions Online
Learn how to test JavaScript regular expressions, inspect matches, understand flags, and avoid common pattern mistakes.
Start with a small sample
Regular expressions are easier to debug when the input text is small and representative. Instead of testing against a full log file or a complete document, copy a few lines that include one value that should match and one value that should not match.
A focused sample helps you see whether the pattern is too strict, too loose, or matching the wrong part of the text.
Understand flags before changing the pattern
JavaScript regex flags can change the result without changing the pattern itself. The global flag finds multiple matches, the case-insensitive flag ignores letter case, and the multiline flag changes how line anchors behave.
If a regex works in one tool but not in your code, compare both the pattern and the flags. A missing global flag or an unexpected multiline setting is a common cause of different results.
- g: find all matches instead of stopping after the first match
- i: ignore letter case
- m: treat each line as a separate anchor target for ^ and $
Use capture groups intentionally
Parentheses create capture groups. They are useful when you need to extract a specific part of a match, such as an ID, slug, date, or domain name.
If you only need grouping for logic, use a non-capturing group so the output is easier to read. Too many accidental capture groups make replacement and extraction code harder to maintain.
- Capture group: /user-(\d+)/ extracts the numeric ID
- Non-capturing group: /(?:jpg|png|webp)$/ groups alternatives without storing a capture
- Named group: /(?<year>\d{4})/ can make extraction code clearer
Common regex mistakes
Many regex bugs come from forgetting to escape special characters. A dot means any character unless it is escaped. Parentheses, brackets, braces, and question marks also have special meanings.
Greedy matching is another frequent surprise. A pattern such as /<.*>/ may capture more text than expected because the star tries to match as much as possible. Use a more specific character class or a lazy quantifier when appropriate.
FAQ
Why does my regex only find one match?
In JavaScript, you usually need the global flag to collect all matches. Without it, many APIs stop after the first match.
Should I use regex to parse HTML?
Use a real HTML parser for full documents. Regex can be useful for small controlled snippets, but it is fragile for nested markup.
Why does a dot match unexpected text?
A dot is a wildcard in regex. Escape it as \. when you want a literal period.