admin管理员组文章数量:1289351
I have a string like below
Original string : results
1apple23oranges : 1 , 23
4apples1oranges : 4, 1
25oranges : 25
By regular expression or any, i can't figure out how to get the above results as pure digits in javascript.
Any idea pls?
I have a string like below
Original string : results
1apple23oranges : 1 , 23
4apples1oranges : 4, 1
25oranges : 25
By regular expression or any, i can't figure out how to get the above results as pure digits in javascript.
Any idea pls?
Share Improve this question edited May 10, 2011 at 6:58 kapa 78.7k21 gold badges165 silver badges178 bronze badges asked May 10, 2011 at 6:55 Kathy001Kathy001 11 gold badge1 silver badge1 bronze badge4 Answers
Reset to default 7Use the regular expression \d+
, which means any digit from 0 to 9 (\d
) repeated one or more times (+
). The qualifier g
will make the search global (ie: don't stop on the first hit).
resultArray = original.match(/\d+/g);
This will result an array with all the numbers, to join them using ", " a separator, use the function join()
resultString = original.match(/\d+/g).join(", ");
'1apple23oranges'.match(/\d+/g);
Use match function on your string object with expression .match(/\d+/g).
Eg. var a = "1apple23oranges"
var res = a.match(/\d+/g)
You can separate each values by ma.
Try "1apple23oranges".match(/(\d+)/g);
Note: If you need the digits as integer value then you have to use parseInt
for that. If you are using jQuery then you can have all the integers in an array
var arr = new Array();
$.each("1apple23oranges".match(/(\d+)/g), function(index, value){ arr.push(parseInt(value, 10));});
本文标签: regexJavascriptExtracting the digits from different positions from a stringStack Overflow
版权声明:本文标题:regex - Javascript : Extracting the digits from different positions from a string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741477377a2380963.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论