Regex for Extracting Only Digits From a String

Matches every non-digit character, typically used with a replace to strip everything but digits.

Published October 13, 2026

Matches every non-digit character, typically used with a replace to strip everything but digits.

Key points

  • Replacing all \D matches with an empty string leaves only the digits, useful for cleaning up phone numbers pasted with formatting

Example

\D
More General articles