开发者

Weird result with string.split() and Regular Expressions

I'm trying to split this string:

values = "43/320x开发者_StackOverflow社区240/99/0/0,34/320x240/9/0/115,18/320x240/9/0/115,5/320x240/7/0/0"

And get only the first number for each "group". Instead of splitting by , and / I'm trying to do it with only one Regular Expression. But when I try this:

values.split(/\/(\d|x|\/)+,?/g)

I get this:

["43", "0", "34", "5", "18", "5", "5", "0"]

Instead of:

["43", "34", "18", "5"]

Why is there an invalid number between each number that I want? It seems that Rails is getting all numbers before and after the comma. I tried with this other RegExp: (/[\d|x]+){4},? and got a similar result.

Is there something wrong that I can't see? Do you know a better solution?


I believe that split returns the capture groups as well. Try using non-capturing parentheses, or better yet, a character class:

values.split(/\/[\dx\/]+,?/g)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜