Regex Code Generator
WebEverything runs locally in your browser — nothing is uploaded
Enter a regular expression, choose a language, and get four ready snippets: match, find all, replace and test. The point of this tool is how many backslashes to write. The same `\d+` needs a different number of them depending on the language, and getting it wrong raises nothing: · In JavaScript `"\d"` is `"d"` — an invalid escape in a string is silently dropped, so your pattern quietly becomes `d` and matches nothing, with no warning at all. Write `"\\d"`, or use a regex literal `/…/`. · Python uses a raw string `r"…"` so no doubling is needed; a plain string now raises a DeprecationWarning. · Java and C# double-quoted strings treat the backslash as an escape, so you must write `\\d`. · In a PHP single-quoted string only `\\` and `\'` are escapes, so `'/\d/'` is correct — doubling there is the mistake. · Go uses a backtick raw string, which also needs no doubling. ⚠️ A pattern that does not compile is rejected here rather than turned into snippets that fail in your project, where the error message is far less clear. ⚠️ The `$1` / `\1` in a replacement is a group reference and its syntax differs (`\1` in Python, mostly `$1` elsewhere); a note says so.
Features
- Six languages: JavaScript, Python, Java, Go, PHP, C#
- ⚠️ Each language gets its own correct escaping, not the same string reused
- Four snippets: match, find all, replace, test
- Snippets are complete and runnable (with imports and declarations), not a bare expression
- Flags are translated per language (`re.IGNORECASE`, `Pattern.CASE_INSENSITIVE`, `RegexOptions.IgnoreCase`)
- ⚠️ A pattern that does not compile is reported immediately
- The generated code does not change with the interface language
- Computed locally
How to use
- Enter a regular expression (without the surrounding slashes)
- Choose the target language; add flags and a replacement if needed
- Four snippets for that language appear immediately
- Before copying, look at the “Inside a string literal” row — that is where mistakes happen
FAQ
- Why does "\d" not work in JavaScript?
- Because an invalid escape inside a string is silently dropped: `"\d"` in JS is just `"d"`, so your pattern ends up matching the letter d. It is the hardest kind of bug to find — no error, no warning, it simply never matches. Two fixes: write `"\\d"` (doubled), or use a regex literal `/\d/` (no doubling inside a literal, which is what the JavaScript snippets here use). Java and C# double-quoted strings behave the same way, except they fail at compile time, which is safer.
- Why no doubling in a PHP single-quoted string?
- Because only `\\` and `\'` are escape sequences in a PHP single-quoted string; every other backslash is preserved as written. So the `\d` in `'/\d/'` reaches the regex engine intact. PHP double-quoted strings, by contrast, treat `\d` as an escape (the result is still d, but a warning is emitted), so doubling is needed there. Two quoting rules in one language is exactly why this tool gives per-language snippets.
- Can I paste these straight into a project?
- Yes — the snippets are complete: they include the import/using and the variable declarations. A few things are yours to adjust: the variable name (`text` is your own string), and the group-reference syntax in a replacement (Python uses `\1`, Go `$1` or the safer `${1}`, most others `$1`). Note also that only the shared flags (i / m / s) are translated — `g` is a JavaScript-specific idea, and other languages express “keep finding matches” as a loop, which is why the find-all snippet for Java is `while (matcher.find())`. The `y` (sticky) flag is JavaScript-only too, and picking it is reported as ignored.
Related tools
Regex Tester
TextTest a regex in the browser: live highlighting, capture groups, replace preview, a backtracking guard — and JavaScript, Python, Java, Go or PHP code.
Code Highlighter
WebColourise code and copy it with the colours intact into slides, Word or email — or export inline-styled HTML for a blog. 26 languages supported.
URL Parser
WebParse a URL into scheme, host, port, path, query parameters and fragment, with each parameter shown raw and decoded. Runs entirely in your browser.
User-Agent Parser
WebParse a user agent into browser, version, OS, device type and engine, and see which rules matched. Pattern matching only. Runs entirely in your browser.
HTTP Status Codes
WebLook up an HTTP status code for its meaning, 1xx–5xx class, typical uses and common traps. Runs entirely in your browser.
MIME Types
WebLook up a MIME type from an extension, or the default extension for a media type. Common types only. Runs entirely in your browser.