Regular Expressions Are Not as Scary as They Look
The first time I saw a regular expression, it looked like this: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
I closed the tab immediately.
Six months later I learned what it does — it validates email addresses. Every single character in that expression has a specific purpose, and once you understand the building blocks, reading and writing regular expressions becomes logical rather than mysterious.
This tool lets you write a regex pattern and test it against any text in real time. Matches highlight as you type, so you see immediately whether your pattern works rather than guessing and running code.
—What Is a Regular Expression?
A regular expression — regex for short — is a pattern that describes a set of strings. You use it to search for text that matches the pattern, validate that input follows a specific format, or extract specific pieces of information from a larger block of text.
Every major programming language supports regular expressions. JavaScript, Python, PHP, Java, Ruby, Go — they all use regex for the same fundamental tasks. The syntax has minor differences between languages, but the core concepts are identical. Learn regex once and it works everywhere.
—The Building Blocks — Regex Syntax Explained Simply
Literal Characters
The simplest regex is just a word. The pattern hello matches the exact text “hello” wherever it appears. Case matters — hello does not match “Hello” unless you use the case-insensitive flag.
The Dot — Match Any Character
A dot . matches any single character except a line break. The pattern h.llo matches “hello”, “hallo”, “h3llo”, and “h!llo”. If you want to match a literal dot, escape it with a backslash: \.
Character Classes — [ ]
Square brackets define a set of characters to match. [aeiou] matches any single vowel. [a-z] matches any lowercase letter. [0-9] matches any digit. [a-zA-Z0-9] matches any letter or digit.
A caret inside square brackets negates the class. [^0-9] matches any character that is NOT a digit.
Quantifiers — How Many Times?
* — zero or more times. a* matches “”, “a”, “aa”, “aaa”.
+ — one or more times. a+ matches “a”, “aa”, “aaa” but not “”.
? — zero or one time. colou?r matches both “color” and “colour”.
{n} — exactly n times. [0-9]{4} matches exactly four digits.
{n,m} — between n and m times. [0-9]{2,4} matches 2, 3, or 4 digits.
Anchors — Position Matters
^ anchors the match to the start of the string. ^Hello only matches text that begins with “Hello”.
$ anchors the match to the end of the string. world$ only matches text that ends with “world”.
Combined: ^Hello world$ matches only the exact string “Hello world” with nothing before or after.
Shorthand Classes
\d — any digit. Equivalent to [0-9].
\w — any word character (letters, digits, underscore). Equivalent to [a-zA-Z0-9_].
\s — any whitespace character (space, tab, line break).
Capital versions negate: \D matches non-digits, \W matches non-word characters, \S matches non-whitespace.
Five Regex Patterns Every Developer Should Know
Email Validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Matches standard email formats. The pattern requires characters before the @, an @ symbol, a domain name, a dot, and a top-level domain of at least two characters.
Phone Number (US Format)
^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$
Matches US phone numbers in formats like (555) 123-4567, 555-123-4567, and 5551234567. The ? after the parentheses and separators makes them optional.
URL Detection
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b
Matches HTTP and HTTPS URLs. The s? makes the S in HTTPS optional, matching both HTTP and HTTPS.
Hex Color Code
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
Matches valid CSS hex color codes in both 6-digit (#ff0000) and 3-digit (#f00) shorthand formats.
IP Address (IPv4)
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
Matches valid IPv4 addresses. The pattern ensures each octet is between 0 and 255.
—Regex Flags — The Settings That Change Everything
Flags modify how the entire regex pattern behaves. The most important ones:
g (global) — Without this flag, regex stops after the first match. With it, the pattern finds every match in the text. Use global when you want to find all occurrences, not just the first one.
i (case insensitive) — Makes the pattern match regardless of letter case. The pattern hello with the i flag matches “hello”, “Hello”, “HELLO”, and “HeLLo”.
m (multiline) — Changes how ^ and $ work. Without multiline, they match only the start and end of the entire string. With multiline, they match the start and end of each line.
How to Use This Regex Tester
Enter your pattern in the regex field. Type or paste the text you want to test in the text area. Matches highlight in real time as you type — no button clicks needed.
Toggle flags using the g, i, and m buttons to change how the pattern matches. The match counter shows how many times the pattern was found in the text.
If your pattern has a syntax error, the tool shows an error message immediately so you can fix it before using the pattern in your code.
—Frequently Asked Questions
Does this regex tester use JavaScript regex syntax?
Yes. The tester runs in your browser using JavaScript’s built-in RegExp engine. JavaScript regex is compatible with most other languages for basic patterns, but some advanced features differ between languages, particularly lookaheads, lookbehinds, and named capture groups.
Why is my pattern matching more than I expected?
This usually means you need anchors. Without ^ and $, a pattern like [0-9]+ matches any sequence of digits anywhere in the text — including numbers inside longer strings. Add ^ at the start and $ at the end to match only when the entire string is digits.
What is the difference between greedy and lazy matching?
Quantifiers are greedy by default — they match as much text as possible. Adding ? after a quantifier makes it lazy — it matches as little as possible. .* is greedy. .*? is lazy. This matters when matching text between delimiters like HTML tags.
Can I use this to replace text, not just find it?
The tester shows matches visually. For replacement, take your tested pattern into your code and use the replace function in your language of choice — JavaScript’s string.replace(/pattern/g, replacement) or Python’s re.sub(pattern, replacement, string).
Is regex the right tool for parsing HTML?
For simple extraction tasks, yes. For parsing full HTML documents, no. HTML is not a regular language and regex cannot reliably handle all edge cases in HTML structure. Use a proper HTML parser for anything beyond simple pattern matching in HTML strings.
—Test It Here. Use It Everywhere.
The fastest way to learn regex is to write patterns and see what they match in real time. Paste some sample text, write a pattern, and watch the highlights appear. Adjust the pattern until it matches exactly what you want and nothing you do not.
A pattern you have tested is a pattern you can trust. Copy it from here and use it in any language.
