| Basics | More | Groups | Quantifiers |
|---|---|---|---|
. Any non-newline character | \n New line | * 0 or more | (...) Capturing group |
\s Any whitespace character | \t Tab | + 1 or more | (?:...) Non-capturing group |
\d Any digit | [abd] Characters a, b, or c | ? 0 or 1 | (a|b) Match a or b |
^ Start of line | [^abd] Any characters except a, b, or c | {2} Exactly 2 | (...)? Optional match group |
$ End of line | [a-z] Any character a to z | {3,6} Between 3 and 6 | (\d\d) Match any two digits |
\ Character Escape | [a-zA-Z] Any character a to z OR A to Z | {4,} 4 or more | (amy) Match word 'amy' |
How to use
Type your regex in the pattern field, with flags such as i or g if you need them. The field turns red and shows the error when the syntax is not valid.
Type up to three values. Each turns green when the pattern matches and red when it does not.
Read the match details under the fields: the matched text highlighted in each value, and every capture group with its content.
Checking many values? Paste the whole list in the batch check further down.
Regex checker: test a pattern as you type
Check a list of values against one regex
That is the whole tool: a pattern, your values, a green or red answer for each one, and what matched. If the syntax is the problem, the guide below goes from a literal match to groups, quantifiers and anchors.
If you are writing patterns to find personal data in your analytics, the list of PII examples by category gives you the formats to match, and what is PII explains which of them matter for compliance.
Frequently asked questions
?How do I check if a regex pattern is valid?
Type it in the pattern field above. If the pattern compiles, the field turns green; if not, it turns red and the error says why, for example an unclosed group or a quantifier with nothing to repeat.
?What regex syntax does this checker use?
JavaScript, the syntax Google Tag Manager uses in its triggers and variables. GA4 filters accept the same patterns except lookarounds and backreferences, so a pattern that uses them works here but not in GA4.
?What is the difference between test() and match() in JavaScript?
regex.test(text) returns true or false: whether the pattern matches anywhere in the text. text.match(regex) returns what matched: the matched text and its capture groups, or every match when the pattern has the g flag. This page uses both: the colour is test(), the match details are match().
?Is my data sent anywhere?
No. The pattern and the values are checked in your browser, and nothing leaves the page.
How to Write a Regex: A Comprehensive Guide to Regular Expressions
Regular expressions, or regex, are a powerful tool for searching, matching, and manipulating text. Whether you're a developer working with JavaScript, a data scientist parsing datasets, or simply someone looking to refine search patterns, understanding how to write effective regex can significantly enhance your productivity. This guide will walk you through the basics of creating regular expressions, from simple matches to complex patterns.
Understanding the Basics of Regex
At its core, a regex is a string of characters used to define a search pattern. It can be used for everything from validating email addresses and phone numbers to parsing large text files. Regex patterns are supported by many programming languages, including JavaScript (JS), Python, and PHP, making them a versatile skill for any coder.
Starting with Simple Patterns
- Literal Characters: The simplest form of regex is a sequence of literal characters, such as
test, which will match the exact string "test" in your search text. - Metacharacters: To unlock the full potential of regex, you'll need to use metacharacters. Characters like
.(dot) match any single character, while*(asterisk) matches zero or more of the preceding element.
Using Character Classes
Character classes allow you to match any one out of several characters at a specific position. For example, [abc] will match any single character a, b, or c. Use ranges to match any character within a range, such as [a-z] for any lowercase letter.
Implementing Quantifiers
Quantifiers let you specify how many instances of a character or group you want to match:
?matches 0 or 1 of the preceding element, making it optional.+matches 1 or more of the preceding element.{n}matches exactly n occurrences of the preceding element.
Grouping and Capturing
Parentheses () are used to group parts of your regex together. This not only allows you to apply quantifiers to the entire group but also lets you capture the matched text for use later.
Advanced Techniques
- Lookahead and Lookbehind: These constructs let you assert what does or doesn't follow or precede your match without including it in the result.
- Non-Capturing Groups:
(?:...)groups part of your regex without capturing the matched text. - Flags: Regex flags like
g(global search),i(case-insensitive), andm(multi-line) modify how the search is performed.
Tips for Writing Effective Regex
- Start Simple: Begin with the basics and gradually add complexity.
- Use Online Regex Testers: Tools like online regex testers are invaluable for experimenting with and debugging your expressions.
- Keep Learning: Regular expressions are a deep topic. Explore more advanced patterns and techniques as you grow more comfortable.
Conclusion
Writing regex is a skill that combines logic, precision, and a bit of creativity. By starting with the fundamentals and progressively tackling more complex patterns, you can harness the full power of regular expressions to make your text processing tasks more efficient and effective. Remember, practice makes perfect, so use online regex testers and validators to refine your skills and ensure your expressions are error-free.