Node.JS Extract Negative And Positive Numbers From The String

Node.JS (JavaScript) Filter Negative Numbers From The Array

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' ]
0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like