1

我有字符串:“您可以为主题投票 36 次,为评论投票 84 次”。

我需要从字符串中获取数字 84(数字可以不同)。

我该怎么做?

4

2 回答 2

8

试试这个:

/^You can vote \d\d times for topics and (\d\d) times for comments$/.exec(str);

或者:

/^You can vote \d+ times for topics and (\d+) times for comments$/.exec(str);
于 2012-06-26T12:18:20.487 回答
2

或者试试这个不同的:demo http://jsfiddle.net/GyK7J/

代码

var s = "You can vote 36 times for topics and 84 times for comments".;
var firstnumber = parseInt(/vote\s(\d+)/.exec(s)[1], 10);
var second = /and\s([\d+]+)/.exec(s)[1];
于 2012-06-26T12:24:03.447 回答