A regular expression describes a text pattern. Regex is useful for search, validation, replacement, log analysis, and extracting structured fragments from larger strings. The notation is compact, so the safest way to build a pattern is one small step at a time.

Use the QuickToolsBox Regex Tester to see matches while you refine a JavaScript-compatible pattern.

Start with literal text

The pattern cat matches those three consecutive characters. By default, it can match inside a longer word such as catalog. If you want a complete word, word boundaries can help:

\bcat\b

Regex flavor matters. Features supported in JavaScript may differ from those in Python, PHP, Java, .NET, or command-line tools.

Character classes

Square brackets match one character from a set:

  • [abc] matches a, b, or c
  • [0-9] matches one ASCII digit
  • [A-Za-z] matches one basic Latin letter
  • [^0-9] matches one character that is not an ASCII digit

Convenience classes such as \d, \w, and \s represent digits, word characters, and whitespace, but their exact Unicode behavior can vary by engine and flags.

Quantifiers

Quantifiers control repetition:

  • ? means zero or one
  • * means zero or more
  • + means one or more
  • {3} means exactly three
  • {2,5} means between two and five

For example, [0-9]{4} matches four consecutive digits. Without anchors, it can also match four digits inside a longer string.

Anchors

^ matches the beginning and $ matches the end. A validation pattern often needs both:

^[A-Z]{2}-[0-9]{4}$

This accepts values shaped like AB-2048 and rejects extra characters before or after them.

In multiline mode, anchors may apply to individual lines rather than only to the entire input. Always test with the flags you plan to use in production.

Groups and alternatives

Parentheses group part of a pattern. The pipe character means “or”:

^(cat|dog)s?$

This matches cat, cats, dog, or dogs. Capturing groups also save matched text for later use. If you only need grouping, a non-capturing group such as (?:cat|dog) can make intent clearer in engines that support it.

Escape special characters

Characters such as ., +, *, ?, (, and [ have special meanings. To match a literal dot, write \.. When a regex is embedded in a programming-language string, the backslash may need to be escaped again.

For example, a regex literal and a string passed to a regex constructor may require different source text even though they create the same pattern.

A practical testing process

  1. Write examples that should match.
  2. Write examples that must not match.
  3. Begin with the simplest literal portion.
  4. Add one class, group, or quantifier at a time.
  5. Test empty input, long input, punctuation, and Unicode.
  6. Confirm the behavior in the actual application language.

Common mistakes

Using .* everywhere

.* is broad and often hides an unclear requirement. Prefer a specific character class and a useful length limit when possible.

Forgetting anchors during validation

A pattern may match a valid fragment inside otherwise invalid input. Anchor the complete value when complete validation is intended.

Catastrophic backtracking

Nested ambiguous quantifiers can take excessive time on certain input. Keep patterns specific, avoid unnecessary nesting, and place reasonable limits on untrusted input.

Validating complex standards with one regex

Email addresses, URLs, and programming languages have edge cases that are difficult to capture with a small pattern. Use a proven parser or validation library when correctness matters.

Frequently asked questions

What does the global flag do?

In JavaScript, the g flag finds multiple matches rather than stopping after the first. It can also affect how repeated calls use the regex object's state.

How do I make matching case-insensitive?

Use the appropriate case-insensitive flag, commonly i. Remember that case behavior can be more complex for international text.

Is regex suitable for parsing JSON?

Regex can locate simple fragments, but a JSON parser is the correct tool for validating and interpreting nested JSON reliably.