Node.JS (JavaScript) Extract Negative And Positive Numbers From The String

We hope you already know the regular expressions as well map

Following Example demonstrate how to extract the negative and positive integers or numbers from the string using Javascript, Node.JS

In the example, We have used regular expressions to extract the negative and positive numbers from the string.

String

//nnndd$$$$123,988,d-113,’’}}]]

Expression:

const extractNumbersFromString = str.match(/-?\d+/g).map(Number);

Expression `/-?\d+/g` will extract globally positive and negative digits from the string

if you run the following example you will get

[ 123, 988, -113 ]

We have also used `map` because if you skip map it will return you String array as follow

[ '123', '988', '-113' ]

Written by

We teach & develop for startups

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store