Complete regular expression reference with syntax, flags, groups, lookaheads, and real-world pattern examples.
Try in Regex Tester β| Syntax | Description |
|---|---|
. | Any character except newline |
\d | Digit [0-9] |
\D | Non-digit [^0-9] |
\w | Word char [a-zA-Z0-9_] |
\W | Non-word character |
\s | Whitespace (space, tab, newline) |
\S | Non-whitespace |
[abc] | One of a, b, or c |
[^abc] | Not a, b, or c |
[a-z] | Character in range aβz |
[a-zA-Z] | Letter (any case) |
\p{L} | Any Unicode letter (u flag) |
| Syntax | Description |
|---|---|
^ | Start of string (or line with m flag) |
$ | End of string (or line with m flag) |
\b | Word boundary |
\B | Non-word boundary |
\A | Start of string (Python/Java) |
\Z | End of string (Python/Java) |
| Syntax | Description |
|---|---|
* | 0 or more (greedy) |
+ | 1 or more (greedy) |
? | 0 or 1 (optional) |
{n} | Exactly n times |
{n,} | n or more times |
{n,m} | Between n and m times |
*? | 0 or more (lazy) |
+? | 1 or more (lazy) |
?? | 0 or 1 (lazy) |
| Syntax | Description |
|---|---|
(abc) | Capturing group |
(?:abc) | Non-capturing group |
(?<name>abc) | Named capturing group |
\1 | Backreference to group 1 |
\k<name> | Named backreference |
(?|...) | Branch reset group (PCRE) |
| Syntax | Description |
|---|---|
(?=abc) | Positive lookahead β must be followed by abc |
(?!abc) | Negative lookahead β must NOT be followed by abc |
(?<=abc) | Positive lookbehind β must be preceded by abc |
(?<!abc) | Negative lookbehind β must NOT be preceded by abc |
| Flag | Name | Description |
|---|---|---|
g | Global | Find all matches, not just the first |
i | Case-insensitive | Match regardless of case (A = a) |
m | Multiline | ^ and $ match start/end of each line |
s | Dotall | . matches newline characters too |
u | Unicode | Enable full Unicode support (\p{} classes) |
y | Sticky | Match only at lastIndex position (JS) |
x | Extended | Allow whitespace + comments in pattern (Python/PHP) |
| Pattern | Regex | Test |
|---|---|---|
[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,} | Try in Regex Tester β | |
| URL | https?:\/\/[\w\-]+(\.[\w\-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=%]* | Try in Regex Tester β |
| IPv4 Address | \b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b | Try in Regex Tester β |
| Phone (US) | \+?1?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4} | Try in Regex Tester β |
| Date (YYYY-MM-DD) | \d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01]) | Try in Regex Tester β |
| Time (HH:MM) | (?:[01]\d|2[0-3]):[0-5]\d | Try in Regex Tester β |
| UUID | [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} | Try in Regex Tester β |
| Hex Color | #(?:[0-9a-fA-F]{3}){1,2}\b | Try in Regex Tester β |
| Slug | [a-z0-9]+(?:-[a-z0-9]+)* | Try in Regex Tester β |
| ZIP Code (US) | \d{5}(?:-\d{4})? | Try in Regex Tester β |
| Credit Card | (?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}) | Try in Regex Tester β |
| HTML Tag | <([a-zA-Z][a-zA-Z0-9]*)(?:\s[^>]*)?\/?>.*?<\/\1> | Try in Regex Tester β |
| Markdown Bold | \*\*([^*]+)\*\* | Try in Regex Tester β |
| JWT Token | [A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+ | Try in Regex Tester β |
| Semver | \bv?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\w.]+)?\b | Try in Regex Tester β |
| GitHub Username | (?<=github\.com\/)([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,37}[a-zA-Z0-9])?) | Try in Regex Tester β |
| Positive Integer | ^[1-9]\d*$ | Try in Regex Tester β |
| Float / Decimal | -?\d+(?:\.\d+)? | Try in Regex Tester β |
| Blank Lines | ^\s*$ | Try in Regex Tester β |
| Duplicate Words | \b(\w+)\s+\1\b | Try in Regex Tester β |
A regular expression is a sequence of characters that defines a search pattern. It is used for string searching, matching, and manipulation. Regex is supported natively in JavaScript, Python, Java, PHP, Go, and most modern programming languages.
Greedy quantifiers (*, +, {n,m}) match as many characters as possible. Lazy quantifiers (*?, +?, {n,m}?) match as few characters as possible. For example, <.+> on '<b>bold</b>' greedy matches the entire string, while <.+?> matches '<b>' and '</b>' separately.
Lookaheads (?=...) and lookbehinds (?<=...) are zero-width assertions β they check for a pattern without consuming characters. Positive lookahead (?=foo) asserts that 'foo' follows. Negative lookahead (?!foo) asserts that 'foo' does NOT follow. They are extremely useful for complex validations.
Core syntax (character classes, quantifiers, anchors) is consistent across most languages. However, features like lookbehind, named groups, and specific flags vary by engine. JavaScript uses PCRE-like syntax without lookbehind in older versions; Python, PHP, and Java support full PCRE. Always test in your target language.