Regex Tester

Test and debug regular expressions in real-time. See matches highlighted, view capture groups, and test replacements.

//g

Replace

Common Patterns

What are Regular Expressions?

Regular expressions (regex or regexp) are patterns used to match character combinations in strings. They are powerful tools for searching, validating, and manipulating text.

Common Regex Metacharacters

CharacterDescriptionExample
.Any charactera.c → abc, aXc
*0 or moreab* → a, ab, abb
+1 or moreab+ → ab, abb
?0 or 1colou?r → color, colour
^Start of line^Hello
$End of lineworld$
\dDigit\d+ → 123
\wWord character\w+ → hello_123
\sWhitespacehello\sworld
[abc]Character class[aeiou]
()Capture group(ab)+
|Orcat|dog

Regex Flags

  • g (global): Find all matches instead of stopping at the first
  • i (case-insensitive): Ignore uppercase/lowercase differences
  • m (multiline): ^ and $ match start/end of each line
  • s (dotall): Dot (.) matches newlines too

Tips for Writing Regex

  • Start simple and add complexity gradually
  • Escape special characters with backslash: \. \* \?
  • Use non-greedy quantifiers (*?, +?) when needed
  • Test with edge cases and unexpected input

Frequently Asked Questions

What are regular expressions?
Regular expressions (regex or regexp) are patterns used to match character combinations in strings. They are powerful tools for searching, validating, and manipulating text. Regex is supported in virtually every programming language.
What's the difference between greedy and lazy matching?
Greedy matching (.*) matches as many characters as possible. Lazy matching (.*?) matches as few as possible. For example, in 'a<b>c<d>', /<.*>/ matches '<b>c<d>' greedily, while /<.*?>/ matches '<b>' lazily.
What do ^ and $ do in regex?
^ asserts the start of a string and $ asserts the end. Without them, patterns match anywhere in the text. For exact validation, use both anchors. For example, /^hello$/ only matches 'hello' exactly, while /hello/ matches 'say hello' too.
What are capture groups?
Capture groups are sections of a regex wrapped in parentheses: (group). They extract specific parts of matched text. For example, /(\d{3})-(\d{4})/ captures the area code and number separately. Use $1, $2, etc. in replacements.
How do I test a regex in JavaScript?
Use the RegExp constructor or literal syntax: new RegExp('pattern', 'flags') or /pattern/flags. Test with regex.test(string) for boolean match, or string.match(regex) for match results. Use regex.exec(string) for iterative matching.
What does the 'g' flag do?
The 'g' (global) flag makes the regex find all matches in the string instead of stopping at the first match. Combined with exec(), it iterates through all matches. Without 'g', only the first match is returned.

Related Tools