.*
Developer
Regex Tester FAQ — Regular Expressions, Patterns & Matching
Answers about regular expressions: syntax, common patterns, flags, lookaheads, and how to test regex online with real-time matching.
Q1 What is a regular expression?
A regular expression (regex) is a sequence of characters that defines a search pattern for matching text. Regex is used in virtually every programming language for validation, search, and text manipulation. Test your patterns with the Regex Tester which shows matches in real-time.
Q2 How do I test a regex online?
Paste your pattern and test string into the Regex Tester. Matches are highlighted instantly as you type. You can toggle flags (global, case-insensitive, multiline) and see capture groups.
Q3 How do I match an email with regex?
A practical email regex:
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/. This covers most valid emails. The full RFC 5322 email regex is extremely complex and usually overkill. For production validation, use a library or send a confirmation email.
Q4 What are regex flags?
Flags modify how the regex engine behaves:
g (global) — find all matches; i (case-insensitive); m (multiline) — ^ and $ match line boundaries; s (dotAll) — . matches newlines; u (unicode) — full Unicode support.
Q5 What is the difference between * and + in regex?
* matches zero or more of the preceding element. + matches one or more. Example: /a*/ matches empty string and "aaa". /a+/ requires at least one "a". ? matches zero or one (optional).
Q6 How do I match a phone number with regex?
US phone:
/^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/. International with country code: /^\+?[1-9]\d{1,14}$/ (E.164 format). Phone formats vary widely — for production use, consider libraries like libphonenumber.
Q7 What is a capture group?
Parentheses
() create capture groups that extract matched text. /(\d{4})-(\d{2})-(\d{2})/ captures year, month, and day from a date. Access groups by index: match[1]. Named groups: (?<year>\d{4}).
Q8 What is a lookahead in regex?
A lookahead asserts that a pattern exists (or doesn't) ahead without consuming characters. Positive:
(?=pattern). Negative: (?!pattern). Example: /\d+(?= dollars)/ matches "100" in "100 dollars" but not "100 euros".
Q9 How do I match a URL with regex?
Basic URL:
/https?:\/\/[\w.-]+(?:\.[\w]{2,})(?:\/[\w./?#&=-]*)?/. For comprehensive URL validation, use the URL constructor in JavaScript (new URL(string)) — regex for URLs is notoriously fragile.
Q10 What does the dot (.) match in regex?
By default,
. matches any single character except a newline. With the s (dotAll) flag, it matches newlines too. To match a literal dot, escape it: \..
Q11 How do I make regex non-greedy?
By default, quantifiers are greedy — they match as much as possible. Add
? to make them lazy: .*?, .+?. Example: for "<b>text</b>", /<.*>/ matches the entire string, but /<.*?>/ matches only "<b>".
Q12 What are character classes in regex?
Character classes match any one character from a set:
[abc] matches a, b, or c. [a-z] matches any lowercase letter. [^abc] matches any character NOT in the set. Shorthand: \d (digit), \w (word char), \s (whitespace).
Q13 How do I replace text with regex?
JavaScript:
'hello world'.replace(/world/, 'regex'). Python: re.sub(r'pattern', 'replacement', text). Use capture groups in replacement: '2024-01-15'.replace(/(\d{4})-(\d{2})-(\d{2})/, '$2/$3/$1').
Q14 What is the difference between regex in JavaScript and Python?
JavaScript uses
/pattern/flags syntax. Python uses raw strings: r'pattern' with re.compile(). Named groups differ: JS (?<name>), Python (?P<name>). Python has re.VERBOSE for commented patterns.
Q15 How do I validate an IP address with regex?
IPv4:
/^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/. This validates each octet (0-255). For IPv6, use a dedicated library.
Q16 What is regex backtracking?
When a regex engine fails to match, it backtracks to try alternative paths. Excessive backtracking (catastrophic backtracking) can freeze programs. Example:
/(a+)+$/ on "aaaaaaaaaaaaaaab" causes exponential backtracking. Prevention: avoid nested quantifiers, use atomic groups.
Q17 How do I match whitespace in regex?
\s matches any whitespace character: space, tab, newline, carriage return. To match only spaces: use a literal space. To match multiple whitespace: \s+. To trim: /^\s+|\s+$/g.
Q18 What is a non-capturing group?
A non-capturing group
(?:pattern) groups elements without creating a capture. Use it when you need grouping for quantifiers or alternation but don't need to extract the match. It's slightly faster than capturing groups.
Q19 How do I escape special characters in regex?
Characters with special meaning must be escaped with backslash:
. * + ? ^ $ { } [ ] ( ) | \. Example: to match a literal dot: \.. In Python, use raw strings to avoid double-escaping: r'\.'.
Q20 What is the best way to learn regex?
Start with basics: literal characters,
., *, +, ?, character classes. Then learn anchors, groups, and alternation. Practice with the Regex Tester using real data.
Free Developer Tools
All tools run in your browser — no signup, no data sent to servers.