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
- Increase the Memory Limit for Your Process
- Why You Should Add “node” in Your Travis Config
- Create a PDF from HTML with Puppeteer and Handlebars
- Create Your Own Custom Error
- Extend Multiple Classes (Multi Inheritance)
- Get a File’s Created Date
- Get a File’s Last Modified/Updated Date
- Write a JSON Object to a File
- How to Create an Empty File
- Generate a Random Number in Range With JavaScript/Node.js
- How to Merge Objects
- Retrieve a Request’s IP Address in Node.js
- Detect the Node.js Version in a Running Process or App
- Install Dependencies for a Specific Package
- String Replace All Appearances
- Remove All Whitespace From a String in JavaScript
- Generate a Random String in Node.js or JavaScript
- Remove Extra Spaces From a String in JavaScript or Node.js
- Remove Numbers From a String in JavaScript or Node.js
- Get the Part Before a Character in a String in JavaScript or Node.js
- Get the Part After a Character in a String in JavaScript or Node.js
- How to Check if a Value is a String in JavaScript or Node.js
- Check If a String Includes All Strings in JavaScript/Node.js/TypeScript
- How to Run an Asynchronous Function in Array.map()
- How to Reset and Empty an Array
- for…of vs. for…in Loops
- Clone/Copy an Array in JavaScript and Node.js
- Get an Array With Unique Values (Delete Duplicates)
- Sort an Array of Integers in JavaScript and Node.js
- Sort a Boolean Array in JavaScript, TypeScript, or Node.js
- Check If an Array Contains a Given Value in JavaScript or Node.js
- Add an Item to the Beginning of an Array in JavaScript or Node.js (Coming soon)
- Append an Item at the End of an Array in JavaScript or Node.js (Coming soon)
- Callback and Promise Support in your Node.js Modules
- Run Async Functions/Promises in Sequence
- Run Async Functions/Promises in Parallel
- Run Async Functions in Batches
- How to Fix “Promise resolver undefined is not a function” in Node.js or JavaScript
- Detect if Value Is a Promise in Node.js and JavaScript
- How to Check if an Object is Empty in JavaScript or Node.js
- How to CamelCase Keys of an Object in JavaScript or Node.js (Coming soon)
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 '
Enjoy!
Mentioned Resources
@supercharge/strings
package on GitHub