Key Takeaways
Anchor when you can
Use ^ and $ for full-string validation, to restrict matches; performance depends on the pattern and input.
Avoid nested quantifiers
Patterns like (a+)+ can backtrack catastrophically, rewrite them where semantics permit. JavaScript does not support possessive quantifiers.
Prefer named groups
(?<year>\d{4}) is self-documenting and survives reordering better than numeric groups.
Use character classes
\d, \w, \s are shorter than [0-9], [A-Za-z0-9_], [ \t\n].
Lookarounds are powerful
Zero-width assertions (?=...) and (?<=...) let you match without consuming.
Test with edge cases
Empty strings, Unicode, leading whitespace, newlines, the test data generator above seeds these for you.
Common Use Cases
Form validation
Email, phone, ZIP, password strength, client and server-side.
Log parsing
Extract timestamps, IPs, status codes from access logs.
Secret scanning
Detect JWTs, API keys and credentials in source code.
Code refactoring
Find candidates for simple replacements; use a syntax-aware refactoring tool for structural code changes.
ETL & data cleaning
Normalize phone numbers, currencies, dates and identifiers.
Security rules
WAF rules, input sanitization, deny-list patterns.
Troubleshooting
My pattern matches nothing, what's wrong?
Check your anchors. ^ and $ require the entire string (or line in multiline mode) to match. Remove them, or enable the m flag to match per line.
Why does . not match newlines?
By default, . matches any character except newline. Enable the s (dotall) flag, or use [\s\S].
My regex hangs the browser.
You likely hit catastrophic backtracking, open the Complexity tab. Replace nested quantifiers like (a+)+ with a+, or use atomic groups in PCRE-flavored engines.
Group 1 is empty but the match isn't.
You used a non-capturing group (?:...). Switch to (...) to capture, or name it with (?<name>...).
Unicode characters don't match \w.
\w only covers ASCII [A-Za-z0-9_]. Use the u flag and Unicode property escapes like \p{L}.
How to use regex find and replace
Type a replacement in Replace with and the replaced text appears under the matches, with a count and matching code for JavaScript, Python, Java and Go. In the replacement, $1 inserts group 1, $& inserts the whole match, $<name> inserts a named group and $$ inserts a literal dollar sign. Turn off the g flag to replace only the first match.
Each language spells these differently. Python uses \g<1> or \g<name>, and Java, C# and Go use ${name} for named groups. The generated snippets already convert the syntax for you.
Why your regex works here but not in another language
| Feature | JavaScript | Python re | Java | Go / Rust |
|---|---|---|---|---|
Lookahead (?=…) | Yes | Yes | Yes | No |
Lookbehind (?<=…) | Yes, any length | Fixed length only | Bounded length | No |
Backreference \1 | Yes | Yes | Yes | No |
| Named group | (?<n>…) | (?P<n>…) | (?<n>…) | (?P<n>…) (newer versions also accept (?<n>…)) |
Possessive a++ | No | Yes (3.11+) | Yes | No |
| Catastrophic backtracking | Possible | Possible | Possible | Not possible (linear time) |
Go and Rust use engines that guarantee linear-time matching, which is why they reject lookaround and backreferences. If a pattern needs those, rewrite it or do a second pass in code.
Stop a regex from freezing your app
A pattern such as (a+)+$ can try an exponential number of paths when the text almost matches. This tester runs each new pattern in a separate worker and stops it after 1.5 seconds, so the page stays usable. To fix the pattern itself, remove nested quantifiers ((a+)+ becomes a+), make alternatives mutually exclusive ((a|a)* is a red flag), and anchor with ^ and $ when validating whole values.