I want to split the numbers out of a string and put them in an array using Regex.
For example, I have a string
23a43b3843c9293k234nm5g%>
and using regex I need to get [23,43,3843,9293,234,5]
in an array
how can i achieve this?
I want to split the numbers out of a string and put them in an array using Regex.
For example, I have a string
23a43b3843c9293k234nm5g%>
and using regex I need to get [23,43,3843,9293,234,5]
in an array
how can i achieve this?
The match()
method retrieves the matches when matching a string against a regular expression
Edit: As suggested by Tushar, Use Array.prototype.map
and argument as Number
to cast it as Number.
Try this:
var exp = /[0-9]+/g;
var input = "23a43b3843c9293k234nm5g%>";
var op = input.match(exp).map(Number);
console.log(op);
var text = "23a43b3843c9293k234nm5g%>";
var regex = /(\d+)/g;
alert(text.match(regex));
You get a match object with all of your numbers.
The script above correctly alerts 23,43,3843,9293,234,5.
see Fiddle http://jsfiddle.net/5WJ9v/307/