Remove Numbers From a String in JavaScript or Node.js

JavaScript comes with handy string methods. RegEx support in JavaScript string methods is helpful in a lot of use-cases. For example, if you want to remove all numbers from a string: JavaScript has your back!

Node.js Series Overview

Strip Numbers From a String

JavaScript comes with built-in methods to remove all numbers from a string. A way to remove numbers is to replace them with an empty string. A regular expression is a good way to find all numbers in the string and replace them:

const greeting = 'Hello Marcus, welcome in 2020';

const withoutNumbers = greeting.replace(/\d+/g, '')

// or (works the same way and is a bit more verbose)
const withoutNumbers = greeting.replace(/[0-9]/g, '')

// withoutNumbers = 'Hello Marcus, welcome in '

The \d RegEx matches any number. The + modifier is a flag to match all adjacent numbers. Using /g finds and replaces all numbers in the string in one go.

Use a Package

I’m the maintainer of the @supercharge/strings package providing convenient string utilities. Stripping numbers from a string are is one of the available functions.

const Str = require('@supercharge/strings')

const withoutNumbers = Str('Hello Marcus, welcome in 2020').stripNums().get()

// 'Hello Marcus, welcome in '


Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.