Regex Common Patterns Cookbook
Copy-paste starters for IPs, hex colors, slugs, and more.
- regex cookbook
- ipv4 regex
- hex color regex
- slug regex
- phone regex
Regex Common Patterns Cookbook
Copy-paste starters for IPs, hex colors, slugs, and more.
Commands
| Command | What it does |
|---|---|
^#(?:[0-9a-fA-F]{3}){1,2}$ | Hex color |
^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$ | IPv4 shape |
^[a-z0-9]+(?:-[a-z0-9]+)*$ | URL slug |
^[A-Z]{1,5}-\d+$ | Ticket key like PROJ-123 |
^\+?[0-9][0-9 ()-]{5,}$ | Loose international phone |
Example
const isSlug = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
isSlug.test('developer-cheatsheet')
FAQ
IPv4 shape vs valid IP?
The pattern allows 999.999.999.999 - split and range-check octets for truth.
Where should these live?
A shared validators module with tests per pattern, not scattered inline across the codebase.
Perguntas frequentes
IPv4 shape vs valid IP?
The pattern allows 999.999.999.999 - split and range-check octets for truth.
Where should these live?
A shared validators module with tests per pattern, not scattered inline across the codebase.