The Regex Tester helps developers validate and debug regular expressions with instant visual feedback. See matches highlighted in real-time, toggle common flags, and reference the built-in cheatsheet for quick syntax lookup.
What is Regular Expression?
Regular expressions (regex) are patterns that describe text matching rules. They're used for validation, search, replace, and text extraction across virtually all programming languages. While regex syntax can seem cryptic, it's an incredibly powerful tool once mastered.
Common Regex Syntax
- Character Classes: . (any char), \d (digit), \w (word char), \s (whitespace)
- Anchors: ^ (start), $ (end), \b (word boundary)
- Quantifiers: * (0+), + (1+), ? (0-1), {n} (exactly n), {n,m} (n to m)
- Groups: (abc) grouping, (?:abc) non-capturing, a|b alternation
- Character Sets: [abc] any of a/b/c, [^abc] not a/b/c, [a-z] range
Understanding Regex Flags
Global (g): Find all matches, not just the first one. Case-insensitive (i): Match regardless of upper/lowercase. Multiline (m): ^ and $ match line starts/ends, not just string starts/ends. These flags change matching behavior significantly.
FAQ
Q: How do I match Unicode characters like Chinese?
A: In JavaScript, use the u (unicode) flag with Unicode property escapes: /\p{Script=Han}/u matches Chinese characters. Without the unicode flag, you can use character ranges like [\u4e00-\u9fa5] to match common Chinese characters.
Q: What's the difference between greedy and non-greedy matching?
A: Greedy quantifiers (*, +, ?) match as much as possible. Non-greedy versions (*?, +?, ??) match as little as possible. For example, with text '<a><b>', the pattern <.*> greedy matches '<a><b>' (entire string), while <.*?> matches just '<a>'.
Q: What's a good regex for email validation?
A: A simple pattern is: ^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$ This matches most emails but isn't RFC-compliant. For production, consider using a well-tested library instead of regex, as email address specification is surprisingly complex.