输入:
“纽约(路透社)”美国股指期货指数周三华尔街小幅反弹,标准普尔500指数期货上涨0.34%,道琼斯指数期货上涨0.12%,纳斯达克100期货上涨0.51% 0921 GMT。“
输出应该是所有数字的数组,包括浮点数。
有点类似thread,但它只提取一个数字。
答案 0 :(得分:8)
这应该这样做:
var text = "NEW YORK (Reuters) U.S. stock index futures pointed to a slight rebound on Wall Street on Wednesday, with futures for the S&P 500 up 0.34 percent, Dow Jones futures up 0.12 percent and Nasdaq 100 futures up 0.51 percent at 0921 GMT.";
console.log(text.match(/(\d[\d\.]*)/g));
您可以过滤掉无效的数字,例如55.55.55
使用以下代码:
var numbers = [];
text.replace(/(\d[\d\.]*)/g, function( x ) { var n = Number(x); if (x == n) { numbers.push(x); } })
答案 1 :(得分:3)
这个正则表达式应该有效:
/[-+]?[0-9]*\.?[0-9]+/g
测试:
"NEW YORK (Reuters) U.S. stock index futures pointed to a slight rebound on Wall Street on Wednesday, with futures for the S&P 500 up 0.34 percent, Dow Jones futures up 0.12 percent and Nasdaq 100 futures up 0.51 percent at 0921 GMT.".match(/[-+]?[0-9]*\.?[0-9]+/g)
返回此数组:
["500", "0.34", "0.12", "100", "0.51", "0921"]