DEV Community

ze he
ze he

Posted on • Originally published at aiforeverthing.com

10 Regex Patterns Every Developer Should Know (With Live Examples)

10 Regex Patterns Every Developer Should Know

Regular expressions are one of those things you use daily but never feel fully comfortable with. Here are 10 patterns that solve real problems, with explanations so you actually understand them.

Test these patterns live: aiforeverthing.com/regex-tester.html — paste the patterns and test strings directly.


1. Email Address

\b[\w.+-]+@[\w-]+\.[\w.]{2,}\b
Enter fullscreen mode Exit fullscreen mode

Test string: Contact alice@example.com or support@dev.io for help.

Breakdown:

  • [\w.+-]+ — local part (letters, digits, dots, plus, hyphen)
  • @ — literal @
  • [\w-]+ — domain name
  • \. — literal dot
  • [\w.]{2,} — TLD (at least 2 chars, allows dots for co.uk)

Note: The perfect email regex is famously complex. This catches 99% of real emails without complexity.


2. URL (HTTP/HTTPS)

https?://[\w-]+(\.[\w-]+)+([/\w.,@?^=%&:~+#-]*[\w@?^=%&/~+#-])?
Enter fullscreen mode Exit fullscreen mode

Test string: Visit https://example.com/path?q=1&sort=asc or http://dev.io

The s? makes https optional, and the capture group at the end handles query strings and fragments.


3. 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
Enter fullscreen mode Exit fullscreen mode

Test string: Server: 192.168.1.1, blocked: 10.0.0.255, invalid: 999.1.1.1

Breakdown of each octet group:

  • 25[0-5] — matches 250-255
  • 2[0-4]\d — matches 200-249
  • [01]?\d\d? — matches 0-199

4. ISO Date (YYYY-MM-DD)

\b\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])\b
Enter fullscreen mode Exit fullscreen mode

Test string: Events: 2024-01-15, 2024-12-31, invalid: 2024-13-01, 2024-01-32

This validates month (01-12) and day (01-31) ranges, not just digit counts.


5. JWT Token

ey[A-Za-z0-9-_]+\.ey[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*
Enter fullscreen mode Exit fullscreen mode

Test string: Token: eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyIn0.abc123

All JWTs start with ey (Base64 of {") in both header and payload. The signature is optional (unsigned JWTs have an empty third segment).

Use the JWT Decoder to inspect the contents after finding tokens.


6. Hex Color Code

#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})\b
Enter fullscreen mode Exit fullscreen mode

Test string: Colors: #FF5733, #fff, #336699, invalid: #GGGGGG, #1234

Matches both 3-digit shorthand (#fff) and 6-digit full (#ffffff) hex colors.


7. Phone Numbers (US)

(?:\+1\s?)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}
Enter fullscreen mode Exit fullscreen mode

Test string: Call (555) 123-4567 or 800-999-0000 or +1 415 555 2671

Handles multiple formats: (555) 123-4567, 555-123-4567, 555.123.4567, +1 555 123 4567.


8. Semver Version

\bv?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][\w-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][\w-]*))*))?(?:\+([\w-]+(?:\.[\w-]+)*))?\b
Enter fullscreen mode Exit fullscreen mode

Test string: Released v1.2.3, v1.0.0-alpha.1, 2.0.0-beta+build.1

Full semver compliance per semver.org, including pre-release and build metadata.


9. Extract JSON Keys

"([^"]+)"\s*:
Enter fullscreen mode Exit fullscreen mode

Test string: {"name": "Alice", "age": 30, "email": "alice@example.com"}

Useful for extracting all keys from JSON when you don't want to parse it (e.g., in log analysis). The capture group 1 contains the key name.


10. Markdown Links

\[([^\[\]]+)\]\(([^)]+)\)
Enter fullscreen mode Exit fullscreen mode

Test string: Read the [documentation](https://docs.example.com) or [API guide](https://api.example.com/docs).

  • Group 1: Link text
  • Group 2: URL

Perfect for extracting all links from Markdown files.


Bonus: Replace with Capture Groups

// Swap first and last name
const swapped = 'Smith, John'.replace(/(\w+), (\w+)/, '$2 $1');
// 'John Smith'

// Format date: YYYY-MM-DD → MM/DD/YYYY
const formatted = '2024-01-15'.replace(/(\d{4})-(\d{2})-(\d{2})/, '$2/$3/$1');
// '01/15/2024'

// Extract domain from email
const domain = 'alice@example.com'.match(/@([\w.-]+)/)?.[1];
// 'example.com'
Enter fullscreen mode Exit fullscreen mode

Quick Reference

Pattern Matches
\d Digit [0-9]
\w Word char [a-zA-Z0-9_]
\s Whitespace
\b Word boundary
+ 1 or more
* 0 or more
? 0 or 1
{3,6} 3 to 6 times
(abc) Capture group
(?:abc) Non-capturing group
(?=abc) Lookahead

Test These Patterns

Copy any pattern above and test it against your own data:

Happy matching!

Top comments (0)