Skip to content
Advertisement

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

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.

Answer

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));
Advertisement