Regex Tester

Test regular expressions with live highlighting. Includes RE2 (Go) compatibility warnings.

//
0 matches
Enter a pattern above to see matches

Quick Reference

.any char (except \n)
\d[0-9] digit
\w[a-zA-Z0-9_]
\swhitespace
^start of string/line
$end of string/line
*0 or more
+1 or more
?0 or 1
{n,m}n to m times
(...)capture group
(?:...)non-capture group
(?P<n>...)named group (Go/RE2)
[abc]character class
[^abc]negated class
a|ba or b

What is a Regular Expression?

A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. It is used to match, find, validate, or extract parts of text. Regular expressions are built into virtually every programming language and are used daily for tasks like form validation, log parsing, data extraction, and find-and-replace in code editors.

For example, the pattern ^\d+$ matches any string containing only digits. The pattern [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]+ matches email addresses.

Different programming languages use different regex engines. JavaScript uses a PCRE-like engine that supports lookaheads and backreferences. Go uses the RE2 engine which guarantees linear-time matching but does not support lookaheads — an important difference when porting patterns between languages.

JavaScript vs Go RE2 Regex Differences

This tester uses the JavaScript regex engine. Key differences when using regex in Go (RE2):

FeatureJavaScriptGo RE2Notes
Lookaheads (?= ?!)SupportedNOT supportedMost common porting issue
Lookbehinds (?<= ?<!)SupportedNOT supportedUse alternative patterns
Backreferences (\1)SupportedNOT supportedRE2 is linear-time only
Named groups (?<name>...)Supported(?P<name>...) syntaxGo uses ?P<name> prefix
Unicode properties (\p{L})SupportedSupportedBoth handle Unicode
Inline flags (?i:...)SupportedSupportedWorks in both
Non-greedy .*?SupportedSupportedSame syntax
\d \w \sSupportedSupported (ASCII only in RE2)RE2 \d = [0-9] only

Common Regex Patterns

PatternMatchesNotes
^[\w.+-]+@[\w-]+\.[\w.]{2,}$Email addressesBasic validation, not RFC 5321 compliant
https?://[^\s]+URLsSimple URL extraction
\b\d{4}-\d{2}-\d{2}\bISO dates (2024-01-15)Works in RE2 and JS
\b(?:\d{1,3}\.){3}\d{1,3}\bIPv4 addressesDoes not validate ranges
[a-fA-F0-9]{8}-(?:[a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}UUIDsWorks in all engines
(?P<year>\d{4})-(?P<month>\d{2})Named groups (Go syntax)Use ?P<name> in Go

Using Regex in Go

go
package main

import (
	"fmt"
	"regexp"
)

func main() {
	// Compile once, reuse many times
	re := regexp.MustCompile(`^[\w.+-]+@[\w-]+\.[\w.]{2,}$`)

	// Test a string
	fmt.Println(re.MatchString("user@example.com")) // true

	// Find all matches
	text := "contact a@b.com and c@d.org"
	matches := re.FindAllString(text, -1)
	fmt.Println(matches) // [a@b.com c@d.org]

	// Named capture groups
	dateRe := regexp.MustCompile(`(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})`)
	names := dateRe.SubexpNames()
	m := dateRe.FindStringSubmatch("Date: 2024-03-15")
	for i, name := range names {
		if name != "" && i < len(m) {
			fmt.Printf("%s = %s\n", name, m[i])
		}
	}
}

Common Use Cases

  • Form validationTest email, phone, URL and postcode patterns before adding them to your codebase.
  • Log parsingBuild patterns to extract fields from structured log lines.
  • Go RE2 portingCheck if a PCRE pattern from Python or PHP will work unchanged in Go's regexp package.
  • Data extractionMatch and capture structured data from unstructured text like API responses or HTML.

Frequently Asked Questions

This tester uses the JavaScript engine which supports lookaheads. Go uses RE2, which doesn't support lookaheads because RE2 guarantees linear-time matching. The tool shows a warning when it detects lookaheads so you know to rewrite the pattern.

In JavaScript, \d matches Unicode digits (0-9 plus digits from other scripts). In Go's RE2, \d is equivalent to [0-9] and only matches ASCII digits. Use [0-9] explicitly for consistent behavior across languages.

Go uses (?P<name>...) syntax for named capture groups, while JavaScript uses (?<name>...). To find the match: re.SubexpNames() returns group names; re.FindStringSubmatch() returns all groups by index.

Go's regexp package always operates globally by default when using FindAll* methods. The equivalent of g flag behavior is achieved by calling FindAllString(text, -1) with -1 meaning 'all matches'.

Related Tools