Regular Expressions: A Beginner's Guide
Learn regex from scratch with clear examples and explanations.
Test and debug regular expressions live
2 articles to help you understand and use this tool effectively
Learn regex from scratch with clear examples and explanations.
Lookaheads, lookbehinds, and other advanced regex features for power users.
Common questions about using the Regex Tester tool
To test a regex: 1) Enter your pattern in the regex field, 2) Add test text in the input area, 3) See matches highlighted in real-time, 4) View capture groups and match details. Add flags like 'g' (global) and 'i' (case-insensitive) as needed.
Common regex flags: g (global - find all matches), i (case-insensitive), m (multiline - ^ and $ match line boundaries), s (dotall - . matches newlines), u (unicode), y (sticky). Combine flags: /pattern/gi for global case-insensitive matching.
Simple email regex: /^[^\s@]+@[^\s@]+\.[^\s@]+$/. This validates basic format. Full RFC 5322 compliance is extremely complex. For production, use established validation libraries or send a confirmation email instead of complex regex.
* matches zero or more occurrences (optional, any count). + matches one or more occurrences (at least one required). Examples: /a*/ matches '', 'a', 'aaa'; /a+/ matches 'a', 'aaa' but not ''. Use * for optional repetition, + for required.
Capture groups () save matched content for later use. Access in JavaScript: match[1], match[2], etc. Named groups: (?<name>pattern) accessed as match.groups.name. Non-capturing groups (?:pattern) group without capturing. Use for extraction and replacement.
Greedy (default) matches as much as possible: /.+/ matches entire string. Lazy (add ?) matches as little as possible: /.+?/ matches minimal content. Example: /<.+>/ on '<a><b>' matches '<a><b>'; /<.+?>/ matches '<a>' then '<b>'.
Escape with backslash: \. \* \+ \? \[ \] \( \) \{ \} \^ \$ \| \\. In JavaScript strings, double-escape: '\\.' for literal dot. Or use new RegExp(string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')) to escape user input.
Lookahead (?=pattern) matches if followed by pattern, (?!pattern) if not. Lookbehind (?<=pattern) matches if preceded by pattern, (?<!pattern) if not. They don't consume characters. Example: /\d+(?=px)/ matches '10' in '10px' but not in '10em'.
Use \b for word boundaries (between \w and \W characters). /\bcat\b/ matches 'cat' but not 'category' or 'bobcat'. \B matches non-boundaries. Word boundaries are essential for matching whole words in text.
Slow regex causes: 1) Catastrophic backtracking from nested quantifiers like (a+)+, 2) Overly broad patterns with .* 3) Excessive alternation. Solutions: use atomic groups, possessive quantifiers, or specific character classes instead of dot-star.