Regex Match Whole String – Add Start and End Anchors

Local only

Fix a valid regex that matches a substring when the requirement is to match the complete input value.

14 chars · 1 line

8 chars · 1 line

Pattern did not match the inputFlags: g

Regex matching behavior

How to make a regex match the whole string

A regex can compile perfectly and still be too permissive. The pattern admin matches any input containing that substring, including superadminuser. Add start and end anchors when the whole value must satisfy the pattern. This tool is free to use. No account or payment is required.

Last updated

What you can do here

  • Valid regex behavior check
  • Substring-versus-whole-string example
  • Anchor comparison
  • Match or No match output
  • JavaScript RegExp semantics
  • Browser-local processing

Steps

  1. Test the current unanchored pattern against a value with extra prefix or suffix text.
  2. Add ^ before the pattern and $ after it when the entire input must match.
  3. Retest both the valid exact value and nearby invalid values so the anchors enforce the intended boundary.

Unanchored regexes search for a matching substring

JavaScript RegExp test() succeeds when any part of the input matches. Anchors change that boundary rule: ^ requires the match to start at the beginning and $ requires it to finish at the end.

Whole-string regex example

The original pattern accepts text around admin; the corrected pattern requires exactly admin.

Too broad

admin

Whole-string pattern

^admin$

Common questions

Frequently asked questions

Why does /admin/ match superadminuser?

Because an unanchored regex searches for a matching substring anywhere in the input.

Are ^ and $ always whole-string anchors?

Without the m flag they describe the input boundaries. With m, they can also match line boundaries.

Should validation regexes usually be anchored?

When the complete value must follow the format, explicit boundaries are usually clearer than relying on a substring match.

Debugging a specific Regex issue? Browse Regex troubleshooting.