Our web & development tool computes regex accurately. Enter your inputs for detailed analysis and optimization tips. Free to use with no signup required.
Reviewed for accuracy by Daniel Agrici, Founder & Lead Developer
Formula
JavaScript RegExp: new RegExp(pattern, flags)
Tests a regular expression against a string and shows all matches. Common: \d (digit), \w (word char), . (any), * (0+), + (1+), ? (0-1).
Worked Examples
Example 1: Find phone numbers
Problem:Pattern: \d{3}-\d{4}, Text: "Call 555-1234 or 555-5678"
Solution:2 matches: 555-1234, 555-5678
Result:2 matches found
Frequently Asked Questions
What are the most common regex patterns?
Email: [\w.-]+@[\w.-]+\.\w+. Phone: \d{3}[-.]?\d{3}[-.]?\d{4}. URL: https?://[\S]+. IP: \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}.
Background & Theory
A regular expression is a compact description of a set of strings. The classical form - literal characters, concatenation, alternation with the pipe, and the Kleene star for zero or more repetitions - describes exactly the regular languages, which a finite automaton can recognize in time linear in the input length with no backtracking at all. JavaScript RegExp is not restricted to that classical form. Shorthand classes such as \d for a digit, \w for a word character, and \s for whitespace are conveniences, but backreferences, lookahead, and lookbehind push the notation beyond regular languages and require a backtracking engine. Flags modify the whole match rather than part of the pattern: g iterates every match, i folds case, m makes the anchors match at line boundaries, s lets the dot match newlines, u enables full Unicode code point handling, and y anchors matching at lastIndex.
The practical caveats concern performance and scope. Nested quantifiers over overlapping alternatives, the textbook case being a pattern shaped like (a+)+ tested against a long run of the same character, can drive a backtracking engine through exponentially many paths. That failure mode is regular expression denial of service, or ReDoS, and it has caused real production outages. Greedy quantifiers match as much as possible and give ground only when forced, so a dot-star fragment routinely swallows more than intended; the lazy forms written with a trailing question mark exist for that reason. Regular expressions also cannot parse arbitrarily nested structures such as HTML or balanced parentheses, because those are not regular languages, and matching email addresses is a known trap since the grammar in RFC 5322 is far more permissive than the short patterns commonly used.
History
The notation traces to the mathematician Stephen Cole Kleene, who in a 1951 RAND report described regular events over the states of a nerve net and introduced the closure operation now called the Kleene star. Ken Thompson brought the idea into computing with a 1968 Communications of the ACM paper describing how to compile a regular expression into machine instructions for efficient searching, work he had implemented in the QED editor and carried into the Unix editors. The Unix utility grep takes its name from the ed editor command g/re/p, meaning globally search for a regular expression and print, a lineage that fixed regular expressions as a core Unix idea through the 1970s.
Divergent dialects followed. Henry Spencer released a widely copied regex library in 1986, and POSIX standardized two flavors, basic and extended regular expressions, in the POSIX.2 standard of 1992. Larry Wall took the notation in a different direction in Perl, adding shorthand character classes, lazy quantifiers, and lookaround, and that dialect was reimplemented by Philip Hazel as PCRE in 1997, becoming the de facto standard for application programming. JavaScript adopted Perl-style syntax when regular expressions were specified in ECMAScript 3 in December 1999. Later editions extended it: ES2015 added the sticky and Unicode flags, ES2018 added dotAll, named capture groups, and lookbehind, and ES2020 added String.prototype.matchAll, the iteration method this tester uses to collect every match rather than only the first.