Khadim Ali Khadim Ali - 1 year ago 62
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

Answer Source

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(''))

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download