Khadim Ali Khadim Ali - 7 months ago 16
jQuery Question

Selecting numbers only in a JavaScript string

I want to select all the digits from a given string. I tried with the code below, but it does not return all the numbers in the string:

var match = /\d+/.exec("+12 (345)-678.90[]");

It only returns
, while I expect it to return


You need to use global flag, it will return you an array of matched data the you can use join() it.

"+12 (345)-678.90[]".match(/\d+/g).join('');

alert("+12 (345)-678.90[]".match(/\d+/g).join(''))