> text regex
Regex tester
Try out a search pattern (regular expression) before you use it in code or in search and replace: matches are highlighted in the text, groups are listed.
Only the pattern, without slashes around it and without flags.
Without g, JavaScript shows only the first match. The flag y (sticky) is left out here on purpose: It searches at a single position only, so the match list would almost always be empty or misleading.
0 of 200,000 characters.
Matches in the text
Match list
Start counts from 0, as JavaScript reports it. An emoji counts as 2 characters.
$1 is group 1, $<name> a named group, $& the whole match, $$ a dollar sign. Empty means: matches are deleted. For a line break, press Enter; a typed \n stays plain text in JavaScript.
Example patterns to take over
One click loads the pattern, flags and sample text. If your own entries are already in the fields, the button asks first. These patterns are only rough checks, and each one says so.
Quick reference: characters, quantifiers, groups, anchors
Characters
.- Any single character except a line break (with the flag s, that too).
\d- A digit from 0 to 9.
\D- Anything except a digit.
\w- A letter from a to z (no accented letters), a digit or _.
\W- Anything except these characters.
\s- Whitespace: space, tab, line break.
\S- Anything except whitespace.
\p{L}- Any letter, including accented ones. Works only with the flag u.
\.\(\$- A special character as a normal character: put a backslash in front.
\n\t- Line break, tab.
Choices in square brackets
[abc]- One of the characters a, b or c.
[^abc]- Any character except a, b and c.
[a-z][0-9]- One character from a range.
Quantifiers
*- Zero times or more.
+- At least once.
?- Zero times or once (optional).
{3}- Exactly three times.
{2,5}- Two to five times.
{2,}means: at least twice. *?+?- As few as possible instead of as many as possible.
Groups and alternatives
(…)- Group. What it matches appears in the match list as group 1, 2 and so on.
(?:…)- Group without a number, only for grouping.
(?<name>…)- Group with a name.
\1\k<name>- Matches exactly what the group matched once more.
a|b- a or b.
Lookahead (preview) and lookbehind (they only look, they consume nothing)
(?=…)- What follows is …
(?!…)- What follows is not …
(?<=…)- What comes before is …
(?<!…)- What comes before is not …
Anchors
^- Start of the text (with the flag m: of each line).
$- End of the text (with the flag m: of each line).
\b\B- Word boundary, or none. Accented letters do not count as word characters here.
In the replacement text
$1$2- Group 1, group 2 and so on (up to 99).
$<name>- Named group.
$&- The whole match.
$`$'- The text before the match, the text after the match.
$$- A dollar sign.
More for text: Compare two versions · Encode Base64, URL and HTML · Count characters, sort lines
Pattern and text stay on this device. Nothing is saved or sent.
How it works
- 01Type the pattern above and paste the test text below. The matches are highlighted in the text as you type, consecutive ones in alternating colors so you can tell them apart.
- 02The match list gives the start, length and text of each match and the content of the groups, with number and name.
- 03Under “Replace with” you see the result right away and can copy it.
The work runs in a separate background process of the page (a web worker), not in the page itself. If a pattern takes longer than 1.5 seconds, it is stopped and the page tells you. This happens with patterns that try far too many paths (“catastrophic backtracking”), for example (a+)+$ against 32 times a and a b. The 1.5 seconds are a fixed limit on your device, not a statement about whether the pattern would be fast enough in your application. If your browser cannot start a worker, the tool does not run anything at all rather than risk the page.
Limits: The test text may be up to 200,000 characters long, the pattern up to 10,000. The match list shows at most 1,000 matches, and single matches and groups are shortened there after 500 characters. When replacing, all matches count, and the result may be at most 1,000,000 characters long. Start and length count from 0 as in JavaScript and in UTF-16 units, so an emoji counts as 2. Without the flag u, a dot matches only one half of it, with u the whole emoji; a single half appears in the match list as \uD83D or similar. Line breaks in the test text count as one character (\n), even if your text comes from Windows: The browser’s text field makes them uniform.
The flags g, i, m, s and u are built in. y is missing on purpose (see above), and so are d (positions of the groups) and v (extended classes). Without g, JavaScript shows and replaces only the first match, and the tool does the same. In PHP, Python and Java there is no flag g: There, the function decides whether you get one match or all of them (for example preg_match_all or re.findall). Python writes named groups as (?P<name>…) and groups in the replacement text as \1. JavaScript allows lookbehind of varying length, while PHP, Python and Java often require a fixed one. Google Sheets (which uses RE2) knows neither lookahead nor lookbehind nor backreferences like \1. In JavaScript, \w knows only a to z, digits and _, no accented letters: For letters of all languages, use \p{L} with the flag u.