•
#developer#regex#javascript#cheatsheet
Regular Expressions 101: Practical Regex Guide & Testing in the Browser
Regular expressions (regex) are one of the most powerful tools in a software developer’s arsenal for string validation, data extraction, search-and-replace, and text parsing. Yet because of their dense, symbolic syntax, regex patterns can be notoriously difficult to write and debug without live feedback.
Essential Regex Cheat Sheet
Character Classes
.: Any character except newline\d: Digit (0-9)\D: Not a digit\w: Word character (a-z,A-Z,0-9,_)\W: Not a word character\s: Whitespace (space, tab, newline)[abc]: Any character in the set (a,b, orc)[^abc]: Any character NOT in the set
Quantifiers
*: 0 or more matches+: 1 or more matches?: 0 or 1 match (or makes a quantifier non-greedy/lazy){n}: Exactly $n$ occurrences{min,max}: Between $min$ and $max$ occurrences
Anchors & Boundaries
^: Start of string (or start of line in multiline mode)$: End of string (or end of line in multiline mode)\b: Word boundary
Flags
g(Global): Find all matches rather than stopping at the first match.i(Case Insensitive): Match regardless of uppercase or lowercase.m(Multiline):^and$match beginning and end of each line.s(DotAll): Allows.to match newline characters (\n).
Practical Real-World Patterns
1. Simple Email Validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
2. URL Extraction
https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&//=]*)
3. Date Formatting (YYYY-MM-DD)
^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$
Test Regex in Real Time with UtilsConsole
The UtilsConsole Regex Tester & Visualizer is designed for instant interactive debugging:
- Real-Time Visual Highlights: Highlights all match occurrences as you type.
- Capture Group Inspector: Inspect named and numbered capture groups with match indices.
- Substitution & Replacement: Preview
$1,$2capture group replacements before executing them in production code. - Common Pattern Cheatsheet: 1-click loading of common patterns for emails, URLs, dates, phone numbers, and IPv4 addresses.
- 100% Client-Side: Private test strings and test data never leave your browser.
Test and refine your patterns with the UtilsConsole Regex Tester.