Common regex patterns

Regex pattern library18 patterns

Regular expressions people actually search for

A short, opinionated library rather than an exhaustive one: 18 expressions covering the checks and clean-ups that come up again and again. Every pattern carries worked examples that are re-run by your own browser when you open it, snippets for three languages, and a note when the expression can be made slow by hostile input.

If you know what you want to match but not what to call it, paste an example into Match your own text above the list. Every pattern in the library runs against it and the list reorders to put the ones that hit first.

Validation

9

Answer yes or no about a whole string — is this an email, a UUID, a semver tag? Anchored at both ends, so a partial hit does not count.

Extraction

5

Pull every occurrence of something out of a larger body of text: links in a document, keys in a JSON blob, quoted strings in a config.

Replace

4

Find the parts you want to rewrite — runs of whitespace, non-digits, HTML comments — so a single `replace` call cleans the string up.

Every patternexpression · group

Regex for Valid Email Address

/^[^\s@]+@[^\s@]+\.[^\s@]+$/i

Validation

Regex for URL Slug

/^[a-z0-9]+(?:-[a-z0-9]+)*$/

Validation

Regex for Strong Password

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z\d]).{12,}$/

Validation

Regex for UUID v4

/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i

Validation

Regex for IPv4 Address

/^(?:25[0-5]|2[0-4]\d|1?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|1?\d?\d)){3}$/

Validation

Regex for Hex Color

/^#?(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/

Validation

Regex for Semantic Version

/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/

Validation

Regex for Credit Card Number Format

/^(?:\d[ -]*?){13,19}$/

Validation

Regex to Extract URLs

/https?:\/\/[^\s"'<>]+/gi

Extraction

Regex to Extract HTML Tags

/<\/?[A-Za-z][^>]*>/g

Extraction

Regex to Extract JSON Keys

/"([^"]+)"\s*:/g

Extraction

Regex to Extract Quoted Text

/"([^"\\]*(?:\\.[^"\\]*)*)"/g

Extraction

Regex to Extract Markdown Links

/\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/g

Extraction

Regex to Replace Repeated Whitespace

/\s+/g

Replace

Regex to Strip Non-Digits

/\D+/g

Replace

Regex to Collapse Blank Lines

/\n{3,}/g

Replace

Regex to Remove HTML Comments

/<!--[\s\S]*?-->/g

Replace

Regex with Nested Quantifiers

/^(a+)+$/

risk · Validation

Why some patterns are flagged

A regular expression with nested quantifiers — (a+)+ is the classic — can take exponential time on an input that almost matches, because the engine retries every way of splitting the string before giving up. If that expression runs on user input, a short crafted string can pin a CPU core. That is catastrophic backtracking, and the denial-of-service it enables is called ReDoS.

1 of the 18 patterns here are flagged. Use Hide backtracking risks in the filters when you are picking something to run against untrusted input, and reach for a real parser when the format is genuinely nested — a regex cannot match balanced brackets at all.

Patterns18
Validation9
Flagged1
Languages3