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
- 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
- How to Run an Asynchronous Function in Array.map()
- How to Reset and Empty an Array
- for…of vs. for…in Loops
- Get an Array With Unique Values (Delete Duplicates)
- 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
Strip Numbers From a String
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 '
\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 '
@supercharge/stringspackage on GitHub