UtilGenie

A beginner’s guide to regular expressions

Literals, classes, quantifiers, anchors, and flags, matched against the JavaScript rules UtilGenie’s regex tester actually runs.

By Muhammad Ali Khan · Updated 2026-11-11

A pattern is a rule for text

A regular expression describes which slices of a string count as a match. The regex tester builds a JavaScript RegExp from the pattern and the flags you type, then lists the full match text. An empty pattern lists nothing. A pattern the engine rejects shows the engine’s error and lists no matches. The text stays in the browser.

The default flags box is g, which means global. With g, every non-overlapping match is listed. Without g, the page keeps only the first match, even if the same text would match again later. Groups exist in the engine, but this page lists match text only, the whole match, not each parenthesized piece on its own line.

Letters mean themselves, until a metacharacter

In the pattern cat, the letters c, a, and t mean those letters. Against cat cat with no flags, the only listed match is the first cat. Add the g flag and both are listed. A period matches any character except a line break, so c.t matches cat and cot. If you need a literal period, write \. so the dot is not the any-character rule.

Other characters that have jobs include ^ $ * + ? ( ) [ ] { } \ and |. If you want one of those as a literal, escape it with a backslash. A stray [ with no closing bracket is an invalid pattern. The tester reports the error instead of a match list.

Classes and the digits in a sentence

\d matches one digit. \d+ matches one or more digits. Against Order 12 and 3 with the g flag, the matches are 12 and 3. The words are not matches. \s matches whitespace. \w matches a letter, a digit, or an underscore in the usual ASCII sense. A class in square brackets matches one character from the list. [A-Z] with g against AbC matches A and C. It does not match b, and it does not match the whole word.

A leading ^ inside a class means “not these.” [^0-9] matches one character that is not a digit. That is different from the ^ anchor outside a class, which means the start of the text. The tester does not explain which one you typed. Try the pattern on a short sample before you trust it on a long file.

Quantifiers and anchors

+ means one or more. * means zero or more. ? means optional, zero or one. {2} means exactly two. {2,4} means two to four. These apply to the character or group immediately before them. \d{2} matches 12 inside 12 and does not match the single 3. Greedy quantifiers take as much as they can and still let the rest of the pattern succeed. That is why .+ can swallow more of a line than you expected.

^ matches the start, and $ matches the end. Without the m flag, ^ is the start of the whole text, not the start of each line. The pattern ^The with g against a two-line string that begins with The on both lines lists only the first The. Add the m flag and both line starts can match. The tester’s flags box is a free string, so m, i, and g can be combined. i makes letters case-insensitive. g is still what decides whether you see every match or only the first.

A small pattern worth keeping

To find integers in a note, \d+ with g is enough, and you have already seen it return 12 and 3 from Order 12 and 3. To find a word only at the start of the text, anchor it. ^cat does not match the cat in the cat. To find a literal price with a dot, escape the dot: \d+\.\d{2} matches 19.99 and does not treat the dot as any character. 1999 would not satisfy that pattern, because the dot is required.

Parentheses group. (cat|dog) matches either word. The tester still shows the full match, so you see cat or dog, not a separate column for the group. If you need the pieces inside the parentheses, this page will not list them. Read the match text, or adjust the pattern so the whole match is the piece you wanted.

Limits of this tester

The page does not replace the text and it does not show a substitution. It highlights matches and lists them. A pattern that can match an empty string, such as a*, can produce a long list of empty matches under the g flag. Prefer + when you mean “at least one.” Catastrophic backtracking is still possible with nested quantifiers on a long line. If the tab stalls, shorten the sample.

The word counter is the page for a length, not for a pattern. It splits words on whitespace and will not apply \d or anchors. Nothing from the tester is uploaded. Clear the boxes if the sample contains a private id, because a match list is still the text you pasted.

Flags change the list

The flags box is passed to the JavaScript RegExp constructor. g lists every non-overlapping match. Without g, cat against cat cat returns one cat. i folds case, so cat with i matches Cat. m lets ^ and $ match at line breaks. You can type gim together. An unknown flag is an invalid pattern, and the page shows the engine’s message rather than an empty success.

The sample sentence on the tester is The quick brown fox jumps over the lazy dog. fox with g matches fox once. \b\w{5}\b with g matches the five-letter words quick, brown, and jumps. A word boundary is the edge between a word character and something else. If the list is empty, drop the boundary and see whether the letters were the problem.

The tester does not replace text

A replacement is a different tool. This page highlights and lists the full match. Parentheses still group, but (\d+) against Order 12 and 3 shows 12 and 3, not a labeled capture. Change the pattern until the whole match is the piece you wanted, or copy the match and edit it yourself.

An empty match is a real result. a* with g can match empty strings between letters, and the list fills with blanks. Prefer a+ for one or more a characters, and \d+ for digits. Order 12 and 3 with \d+ and g returns 12 and 3 and no empty row.

Five checks before you trust a pattern

Run them in order. fox with g on the sample sentence returns one match. [A-Z] with g on AbC returns A and C. ^The without m on two lines that both start with The returns one match. The same pattern with m returns both lines. An unclosed bracket returns an error, not a silent empty list. If a step disagrees, look at the flags box first. A leftover i or m changes the next try.

The word counter will not apply these rules. It splits words on whitespace. Paste a match there only to measure that slice. Clear the tester when the sample includes a private id. The pattern and the text stay in the browser.

Tools used in this guide

More guides: Compress images for the web, How GPA is calculated.