Test a regex in JavaScript
Type a pattern and sample text in the tool above to see matches highlighted. In code, re.test(str) returns true or false, str.match(re) returns the matches and str.matchAll(re) iterates all of them with positions.
Last reviewed 2026-11-04.
Uses your browser's own RegExp engine (ECMAScript), in a background worker. A run that takes longer than 1 second is stopped, so a pattern that backtracks catastrophically cannot freeze this tab.
Worked example
$ /^\d{3}-\d{4}$/.test('555-1234') // true
/^\d{3}-\d{4}$/.test('x555-1234') // false
'2026-11-04'.match(/(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})/).groups
// { y: '2026', m: '11', d: '04' }
[...'a1b22c333'.matchAll(/\d+/g)].map(m => m[0] + '@' + m.index)
// ['1@1', '22@3', '333@6']The global flag trap
With the g (or y) flag, test() remembers lastIndex between calls on the same regex object:
$ const re = /a/g;
re.test('aa') // true
re.test('aa') // true
re.test('aa') // false (lastIndex was past the end)Drop the g flag when you only need a yes or no, or create the regex inside the function.
Flags
gall matches,iignore case,m^and$match at each line.slets.match a newline:/a.c/.test("a\nc")is false,/a.c/s.test("a\nc")is true.ufull Unicode matching,dadds match indices.
Common mistakes
- Building a regex from a string and forgetting to double the backslash:
new RegExp("\\.")matches a dot,new RegExp("\.")matches any character. - Nested quantifiers such as
(a+)+can freeze the page; see catastrophic backtracking.
Running this on a server
The tester runs in your browser. If your regex validates input on a server, the hosting guide compares small hosts.
Frequently asked questions
What is the difference between test() and match()?
test() returns a boolean. match() returns the matched text and groups, or null.
Why does test() alternate between true and false?
The g flag keeps lastIndex on the regex object. Remove g or reset lastIndex to 0.
Does this use the JavaScript engine?
Yes. It runs your browser's own RegExp, so results match what your code will do.
Sources
- MDN: RegExp.prototype.test() checked 2026-11-04
- MDN: Regular expressions checked 2026-11-04