Regex to split a number from a string

How to split and choose which number is used with a regular expression. The user can enter a string like:

1dozen 3 dozen1 dozen1 <= unlikely, but suppose the user also prints this value

30 kg /

I still discover with incomplete:

/[az](?=\d)|\d(?=[az])/i 

But there is no space or slash. Can anybody help me?

+4
source share
2 answers

There are no unnecessary links!

See http://jsfiddle.net/5WJ9v/

Code:

 var text = "1dozen 3 dozen dozen1 30/kg"; var regex = /(\d+)/g; alert(text.match(regex)); 

You get a matching object with all your numbers.

The script above correctly warns 1,3,1,30 .

+12
source
 var str = '1dozen 3 dozen dozen1 30/kg'; str.match(/\d+/g); // ["1", "3", "1", "30"] 
+2
source

All Articles