What Is Regex, and When Should You Actually Use It?
Learn the core regular expression operators, capture groups, flavor differences, and four signs a proper parser is safer than regex.
Regular expressions (regex) match and extract text patterns using tokens like ., *, +, character classes [a-z], anchors, and capture groups. Avoid regex for nested HTML or JSON—use a dedicated parser instead. Test patterns safely in the Regex Tester.
If you have ever typed *.pdf in a file search box, you have used a glob. A regex is a glob on steroids: a tiny language for matching patterns in text. The pattern ^[w.+-]+@[w-]+.[w.-]+$ matches an email address. The pattern d{3}-d{4} matches a US phone number. The pattern <imgs+[^>]*src="([^"]+)" extracts the URL from an HTML image tag.
Regex is one of the most useful tools in a programmer's toolbox, and one of the most overused. The same pattern that elegantly extracts all the email addresses from a document is the same pattern that becomes a 200-character unmaintainable mess when you try to parse HTML with it. Knowing when to use regex is as important as knowing how.
The four operators that cover 90% of use cases
If you learn four operators, you can write most of the regexes you will ever need. The other 30+ operators are for edge cases.
1. . (dot): any single character. a.c matches abc, a1c, a c, and a&c. It does not match ac (no character between a and c). To match a literal dot, escape it: a.c.
2. * (asterisk): zero or more of the previous character. ab* matches a, ab, abb, abbb. Useful for matching optional suffixes: https?:// matches both http:// and https://.
3. + (plus): one or more of the previous character. Like * but requires at least one match. ab+ matches ab, abb, abbb, but not a.
4. [...] (character class): one of the characters inside the brackets. [aeiou] matches any vowel. [a-z] matches any lowercase letter. [a-zA-Z0-9] matches any alphanumeric. [^0-9] (caret at the start) matches anything that is NOT a digit.
Combine these with anchors (^ for start of line, $ for end of line) and you can write most of the regexes you will need. A few more operators are worth knowing: d (digit), w (word character: letter, digit, underscore), s (whitespace), and (...) (capture group, for extracting a substring).
How to test a regex before you ship it
The single biggest reason for broken regexes is not testing them against real input. A regex that works on test@example.com might fail on test+tag@sub.example.co.uk. A regex that works on the docs example might fail on the malformed user input in production.
The Uttir Regex Tester lets you paste a pattern, paste some test text, and see every match highlighted. The test text should include the happy path, the edge cases, and the failure cases you want to defend against. If you are writing a regex to extract email addresses, your test text should include simple addresses, addresses with plus tags, addresses with subdomains, and a few non-addresses to confirm the regex does not match them.
Test cases for an email regex should at minimum include: a@b.co (minimal valid), user.name+tag@example.co.uk (real-world complex), user@localhost (no TLD, sometimes valid), user @example.com (space, should not match), and @example.com (no local part, should not match). If the regex passes all five, it is probably ready.
Capture groups (the part most tutorials skip)
Matching is one thing. Extracting is another. A regex with parentheses around a part of the pattern captures that part as a separate match. The regex (d{4})-(d{2})-(d{2}) matches a date in YYYY-MM-DD format, and the matches return the year, month, and day separately.
In code, you access the captures by index. In JavaScript: match[1] is the year, match[2] is the month, match[3] is the day. In Python: match.group(1), match.group(2), match.group(3). In sed and awk, the captures are \1, \2, \3 in the replacement string.
Without capture groups, you would have to do additional string manipulation (split, slice, indexOf) to extract the parts. With them, the regex returns the parts as a structured value. The Regex Tester shows the captures separately, so you can see exactly what your code will receive.
The four signs you should not use regex
- You are parsing HTML or XML. HTML is not a regular language. It has nested structures, optional tags, and self-closing tags that make regex either impossible or 200-character monstrosities. Use a parser:
DOMParserin the browser, BeautifulSoup in Python, Nokogiri in Ruby. - You are parsing JSON or YAML. Same reason. Use a parser:
JSON.parse()in JavaScript,json.loads()in Python,yaml.safe_load()in Python. Regex will fail on edge cases (escaped quotes, nested objects, scientific notation in numbers). - You are parsing a natural language. "Find the first sentence in this paragraph" sounds like a regex job, but it is not. Abbreviations, decimal numbers, and quoted text all break naive sentence-detection patterns. Use a real sentence segmenter (NLTK, spaCy) or accept that the heuristic will be wrong sometimes.
- The regex is over 100 characters. A regex longer than that is probably trying to do too much. Step back. The right answer is usually to split the regex into multiple steps, or to use a real parser.
Common regexes you will write more than once
Email address (loose): ^[w.+-]+@[w-]+.[w.-]+$. This is the "good enough" pattern that catches most real emails. The fully correct email regex is 6,000+ characters and maintained by the browser as a built-in.
URL: https?://[w.-]+(?:/[w./%?=&-]*)?. Loose, but works for most cases. A stricter version uses [a-zA-Z][a-zA-Z0-9+.-]*:// for the scheme.
ISO date: ^d{4}-d{2}-d{2}$. Just the date part. The full ISO 8601 with time and timezone is much longer; this catches YYYY-MM-DD and stops there.
IP address (IPv4): ^(d{1,3}.){3}d{1,3}$. Matches the shape. To validate that each octet is 0-255, you need a more complex pattern or a small post-match check.
Hex color: ^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$. Matches #FFF, #FFFFFF, FFF, FFFFFF.
Test each of these in the Regex Tester with the kind of input you actually expect, and you will be fine.
Regex flavors (the part that breaks you at 2 AM)
Regex is not a single standard. JavaScript, Python, Perl, PCRE (PHP), .NET, and Java all implement slightly different regex engines. Most of the common features (the four operators above, character classes, captures) work the same. Some features are different:
- Lookbehind (
(?<=foo)bar): supported in JavaScript, Python, .NET, but NOT in some older regex engines. - Named captures (
(?<name>...)): supported in most modern engines, not in some older ones. - Unicode property classes (
p{L}for any letter): supported in PCRE, Java, Python, but limited in JavaScript (which is why many tools use a polyfill). - Backreferences (
\1): supported almost everywhere, but the syntax for non-capturing groups differs ((?:...)is standard).
If you are writing a regex for one specific environment, you are fine. If you are writing a regex that runs in multiple environments, test it in all of them. The Uttir Regex Tester uses JavaScript regex, which is the most common for client-side code.
When you have learned enough
You have learned enough regex when you can read a 30-character pattern and understand what it matches and what it does not. The bigger the pattern, the less likely you are to read it correctly six months later. A short, well-tested pattern is better than a long, comprehensive one. Add a comment. Test with the tester. Ship it.
Further reading
Primary source: ECMA-262 — Regular Expression Objects.
Frequently asked questions
- Why should you avoid parsing arbitrary HTML or XML with regex?
- HTML and XML allow arbitrarily nested elements, comments, and quoted attributes—structures that are context-free rather than regular languages. Use the browser’s DOMParser or a real HTML/XML parser, and reserve regex for flat token checks tested in the Regex Tester.
- What causes catastrophic backtracking (ReDoS) in a regular expression?
- Nested quantifiers like
(a+)+on ambiguous input force NFA regex engines (used by JavaScript, Python, and PCRE) to explore exponentially many paths before failing. Avoid nested quantifiers and anchor patterns explicitly. - Are JavaScript regular expressions identical to Python or PCRE?
- Basic syntax overlaps, but flags and features differ—for instance, lookbehind support, Unicode property escapes
\p{L}, and whether dot-all/sor multiline/mflags alter.and^/$behavior.