What a regular expression actually is
A regular expression (regex) is a compact pattern language for describing text to match. Rather than searching for one exact string, a regex can describe a shape of text: "a sequence of digits," "an email-like pattern," "anything that starts with a capital letter." This makes regex powerful for validation, extraction, and find-and-replace operations that would otherwise require writing custom logic character by character.
The tradeoff is that regex syntax is dense and easy to get subtly wrong, a pattern that seems to work on your test cases can fail on edge cases you didn't think to try, which is exactly why testing a pattern against varied sample text before using it in production code matters.
How to use this regex tester
- Enter your pattern in the pattern field. No need to include the surrounding slashes, just the pattern itself.
- Set any flags you need, like
gfor matching all occurrences rather than just the first, orifor case-insensitive matching. - Paste or type sample text into the test area.
- Matches are highlighted directly in the text, and the match count updates live as you adjust the pattern.
- If the pattern is invalid, an error message explains what's wrong rather than silently failing.
Testing happens entirely in your browser using JavaScript's native regex engine. The same engine your pattern will actually run against if you're using it in JavaScript code.
Common regex patterns and what they do
\d+ matches one or more digits in a row, useful for finding numbers embedded in text. \b[A-Z][a-z]+\b matches whole words that start with a capital letter followed by lowercase letters, a rough (not perfect) way to find capitalized words like proper nouns. ^\s*$ matches a line that's empty or contains only whitespace, useful for detecting blank lines.
A genuinely reliable email-matching regex is more complex than most people expect, because the technical specification for valid email addresses allows for far more variation than typical examples suggest. For anything where correctness truly matters (like validating user input before sending an email), a well-tested existing pattern or a dedicated validation library is usually safer than writing one from scratch.
Why testing against varied examples matters
A pattern that correctly matches your first few test cases can still have blind spots, for instance, a phone number pattern built only against a specific formatting style might fail on numbers with different spacing, dashes, or country codes. Before relying on a pattern in real code, it's worth testing it against the messiest, most varied realistic inputs you can think of, not just the clean examples that come to mind first.
It's equally useful to test what a pattern shouldn't match. Confirming it correctly rejects invalid input is just as important as confirming it accepts valid input, especially for anything used in validation.