Regex Tester: how it works and how to build one
Test JavaScript regular expressions with live highlighting. This guide explains what happens behind the button and shows the code, so you can build your own.
What it does
Write a pattern, set flags and see every match highlighted in your test text, with capture groups listed for each match. Try a replacement string with $1 references before you put it in code.
When to use it
- Building validation rules
- Extracting data from logs
- Learning regular expressions
How to use it
- Type a pattern and choose flags.
- Paste test text or open a file.
- Read matches and groups, or try a replacement.
Try it free in your browser, nothing is uploaded: https://eskillsfy.com/tools/regex-tester
How it works
The tester compiles your pattern with new RegExp(pattern, flags) and runs it over the text with matchAll, which returns every match with its position and capture groups. Matches are highlighted by slicing the text at those positions.
Invalid patterns throw a SyntaxError, which is shown instead of crashing.
Build it yourself
Here is the core of the tool. It has no dependencies unless noted, so you can paste it into any TypeScript or JavaScript project.
regex.ts (ts)
export function testRegex(pattern: string, flags: string, text: string) {
const re = new RegExp(pattern, flags.includes("g") ? flags : flags + "g");
return [...text.matchAll(re)].slice(0, 1000).map((m) => ({
match: m[0],
index: m.index!,
groups: m.slice(1),
named: m.groups ?? {},
}));
}
Make it production-ready
- Guard against zero-length matches looping forever.
- Add a replace preview with $1 references.
Questions
Which regex flavour is this?
JavaScript (ECMAScript), the same engine Node.js and browsers use.
Free, runs in your browser, nothing is uploaded.