Regex to Strip Non-Digits

All patterns
Replace3 chars · /g

Regex to Strip Non-Digits

This is useful when cleaning phone numbers, account references, or IDs into a digits-only form before formatting or validation.

The expression

/\D+/g

How to read it

gglobal — keep going after the first match instead of stopping

Find matches in your own text

no input
Try:

This expression is unanchored, so it finds every occurrence anywhere in the text. Highlighted runs are what a replace call would rewrite.

Examples, checked in your browser

4/4 verified

(555) 123-4567

Matches 3 times.

INV-2025-0042

Matches 2 times.

123456

No match — this is the kind of input the pattern rejects.

007

No match — this is the kind of input the pattern rejects.

Use it in code

3 languages
JavaScript
const regex = /\D+/g;
regex.test(input);
Python
import re
pattern = re.compile(r"\D+")
bool(pattern.search(input))
Go
re := regexp.MustCompile("\\D+")
matched := re.MatchString(input)

Other replace patterns

3
GroupReplace
Flags/g
Examples4
SafetyOK