[SOLVED] How to select all the numbers from a comma separated and spaces string?

Issue

How to select all the numbers from the below string?

str = "1,2, 4,5 ,6 7 8 9 10, 11, 13"

I tried using split(‘,’) but it doesn’t work for spaces.

As it contains spaces as well.

Solution

Just make a regular expression and match on numbers

console.log("1,2, 4,5 ,6 7 8  9 10, 11, 13".match(/\d+/g).map(Number));

Answered By – epascarello

Answer Checked By – Mary Flores (BugsFixing Volunteer)

Leave a Reply

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