Regex Tester
Complete Regular Expression Guide
1. Regular Expression Basics
Regular expressions (Regex) are powerful tools for defining, searching, extracting, and replacing string patterns. Match literally (\d for digits, \w for word characters) or use special characters for complex patterns. Example: ^[a-z]+$ means strings containing only lowercase letters. ^ is start, $ is end, [a-z] is a to z, + means one or more. Supported in almost all programming languages and text editors, essential for input validation, data parsing, and text replacement.
2. Common Pattern Collection
Frequently used regex patterns in practice: Email: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$, Phone (US): ^(\+1)?[-.\s]?\(?[0-9]{3}\)?[-.\s]?[0-9]{3}[-.\s]?[0-9]{4}$, URL: ^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b, Date (YYYY-MM-DD): ^\d{4}-\d{2}-\d{2}$, Password (min 8 chars, upper+lower+digit+special): ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$. Use these patterns as a base and modify for your project.
3. Regex Flags Explained
Regex flags control search behavior. g(global): finds all matches, not just the first. i(ignoreCase): case-insensitive matching (/hello/i matches Hello, HELLO). m(multiline): ^ and $ match start/end of each line. s(dotAll): . matches newline characters (\n). u(unicode): properly handles full Unicode (emojis, etc.). y(sticky): starts search only at lastIndex position. Example: /hello/gi finds all hello ignoring case.
4. Capture Groups and Backreferences
Parentheses () create groups that capture matched portions for later use. Example: /(\d{4})-(\d{2})-(\d{2})/ where $1 is year, $2 is month, $3 is day. JavaScript's match() result array includes capture groups. Backreferences \1, \2 match the same text as previous groups. Example: /(\w+)\s+\1/ matches "hello hello". Non-capturing groups (?:) group without capturing for better performance.
5. Lookahead and Lookbehind
Lookahead and lookbehind check for patterns before/after without including them in the match. Positive lookahead (?=): /\d(?=px)/ matches digits before px ("100" in "100px"). Negative lookahead (?!): /\d(?!px)/ matches digits not before px. Positive lookbehind (?<=): /(?<=\$)\d+/ matches digits after $ ("100" in "$100"). Negative lookbehind (?
6. Regex Performance Optimization
Complex regex can cause performance issues. Be as specific as possible ([a-z]+ rather than .*). Replace unnecessary capturing groups with non-capturing groups (?:). Use lazy quantifiers (*?, +?) instead of greedy (*,+) to reduce backtracking. For large texts, string methods (indexOf, startsWith) may be faster than regex. Pre-compile regex for reuse (don't repeatedly create new RegExp). Beware of ReDoS (Regular Expression Denial of Service) attacks - never use user input directly as regex patterns.