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

Onexlab
Onexlab
Published in
May 29, 2019

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' ]

--

--