输入:
路透纽约3月23日电-美国股指期货周三显示,华尔街股市略有反弹,标准普尔500指数期货上涨0.34 %,道琼斯期货上涨0.12 %,纳斯达克100指数期货上涨0.51 %,至格林尼治时间0921 GMT.“
输出应该是包括浮点数在内的所有数字的数组。
有点类似的thread,但它只提取一个数字。
发布于 2010-10-20 11:33:02
这应该可以做到:
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); } })发布于 2010-10-20 11:41:57
这个正则表达式应该工作:
/[-+]?[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"]https://stackoverflow.com/questions/3977256
复制相似问题