Regular expressions (regex) are one of the most powerful tools in a developer's toolkit. They let you search, match, validate, and transform text with precision. While regex syntax can feel intimidating at first, mastering the basics opens up efficient solutions for data validation, log parsing, text extraction, and more.
| Pattern | Description | Example |
|---|---|---|
. | Any character (except newline) | h.t matches "hat", "hot" |
\d | Any digit (0-9) | \d{3} matches "123" |
\w | Word character (a-z, A-Z, 0-9, _) | \w+ matches "hello_2" |
\s | Whitespace (space, tab, newline) | \s+ matches spaces |
^ | Start of string/line | ^Hello matches at start |
$ | End of string/line | end$ matches at end |
* | 0 or more repetitions | ab*c matches "ac", "abc" |
+ | 1 or more repetitions | ab+c matches "abc", "abbc" |
? | 0 or 1 (optional) | colou?r matches both spellings |
{n,m} | Between n and m repetitions | \d{2,4} matches 2-4 digits |
[abc] | Character class (a, b, or c) | [aeiou] matches vowels |
[^abc] | Negated class (not a, b, c) | [^0-9] matches non-digits |
(group) | Capture group | (\d+)-(\d+) captures both |
a|b | Alternation (a or b) | cat|dog matches either |
\b | Word boundary | \bword\b whole word only |
| Flag | Name | Effect |
|---|---|---|
g | Global | Find all matches, not just the first |
i | Case-insensitive | /hello/i matches "Hello", "HELLO" |
m | Multiline | ^ and $ match line starts/ends |
s | Dotall | . matches newlines too |
u | Unicode | Enables full Unicode matching |
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
// Matches: [email protected], [email protected]
// Fails: @example.com, user@, [email protected]
Note: email validation via regex is inherently imperfect. For production systems, send a verification email rather than relying solely on regex.
^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$
// Matches: https://devtoolkit.ink/tools/regex
// Matches: http://example.com?q=test&page=1
^\+?[1-9]\d{1,14}$
// E.164 format
// Matches: +14155552671, +905551234567
// Fails: 0555-123-4567 (local format)
^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$
// Matches: 192.168.1.1, 10.0.0.255
// Fails: 256.1.1.1, 192.168.1
For IP network calculations, check out the Subnet Calculator.
^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$
// Matches: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
// Matches: 2026-03-29, 2024-12-01
// Fails: 2024-13-01, 2024-00-15
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
// Requires: lowercase, uppercase, digit, special char, min 8 chars
For generating secure passwords that match these requirements, use the Password Generator.
Pattern: https?://[^\s]+
Flags: g
Input: Visit https://devtoolkit.ink and http://example.com for tools.
Matches: ["https://devtoolkit.ink", "http://example.com"]
Pattern: (\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})
Flags: g
Input: 2026-03-29 14:30:00 [INFO] Server started
Group 1: "2026-03-29"
Group 2: "14:30:00"
Pattern: \b(\w+)\s+\1\b
Flags: gi
Input: The the quick brown fox fox jumped
Matches: "The the", "fox fox"
(a+)+$ can cause exponential performance on certain inputs. Be specific with quantifiers.(?:...) when you do not need the match content. This saves memory.^ and $ when validating entire strings. Without anchors, partial matches slip through.[abc] is faster than a|b|c.Regex flavors differ between languages. JavaScript uses /pattern/flags syntax, while Python uses raw strings r"pattern". Some features like lookbehinds have varying support. Always test in the target language.
Escape special characters with a backslash: \. for a literal dot, \[ for a literal bracket, \\ for a literal backslash.
.* is greedy (matches as much as possible). .*? is lazy (matches as little as possible). For example, on <b>bold</b>, the pattern <.*> matches the entire string, while <.*?> matches only <b>.
DevToolKit — 42+ Free Online Developer Tools