【发布时间】:2011-05-14 15:04:21
【问题描述】:
我真的很难找到解决方案,我想在一段文本中找到一个数字的每个实例。
<div id='text'>
this is the number 20px and this is the number 100 and this is the number 10.
</div>
所以我想接受这个并让它输出以下内容。 20 100 10
<script type="application/javascript">
$(document).ready(function() {
var text = $('.text').text().toString().search( new RegExp( /^[0-9]+$/ i ) );
alert(text);
});
</script>
从警报中,我只希望它输出文本中的数字,即 20 100 10,我现在已经很远了,但是任何有助于让我朝着正确方向前进的帮助我都在用头撞墙,谢谢。
【问题讨论】:
-
你的模式应该是
/\d+?/,现在想不出合适的函数。 -
如果有负数(-100)你应该捕获-100还是100?
-
伟大的点 Mikael 我也修改了我的正则表达式,用于负数和十进制数。 :)
标签: jquery replace numbers find