Guide
Testing regular expressions safely
By Logappradeep · ServiceNow developer
A regular expression is a compact pattern language for finding text. A few characters can describe an email address, a date, a log line or a URL slug. That power is also what makes regex hard: one misplaced quantifier can match too much, too little, or hang your program. A tester lets you try a pattern against real sample text and see exactly what it matches before you put it in code.
How this tester works
The tool compiles your pattern with the browser's own JavaScript RegExp engine, the same one your front-end and Node.js code use. With the g flag it finds every match in the test text; without it, it reports only the first. For each match you see the matched text and the value of each numbered capture group. Invalid patterns show the engine's error message so you can fix them immediately.
Take the default pattern (\w+)@(\w+\.\w+) against support@toolkithub.in. \w+ matches one or more word characters, @ is literal, and \. is an escaped dot. The full match is the address, group 1 is support and group 2 is toolkithub.in.
The four flags
- g (global): find all matches instead of stopping at the first.
- i (ignore case):
abcalso matchesABC. - m (multiline):
^and$match at the start and end of each line, not just the whole string. - s (dotAll):
.also matches newline characters.
Pitfalls developers actually hit
Catastrophic backtracking. Nested quantifiers such as (a+)+$ or (.*,)* can force the engine to try an exponential number of paths when a match fails. Against a string like 30 a characters followed by !, the pattern above can freeze a browser tab or, on a server, become a denial-of-service vector known as ReDoS. Prefer specific character classes like [^,]* over .*, and avoid repeating a group that itself repeats.
Greedy versus lazy. <.+> on <b>hi</b> matches the whole string because + is greedy. Add ? to make it lazy: <.+?> matches each tag separately.
Zero-length matches. Patterns like \b or a* can match an empty string. With the global flag, code that loops over exec() must advance lastIndex manually or it will loop forever; this tester handles that for you.
Flavor differences. JavaScript regex is not identical to PCRE, Python or Java. For example, JavaScript has no possessive quantifiers or atomic groups, and older engines lacked lookbehind. A pattern copied from another language may behave differently here, so test it in the engine you will deploy to.
Everything runs in your browser. Your pattern, flags and sample text are saved in local storage so they are still there next time.