我真的很难找到解决方案,我想找到一段文本中数字的每个实例。
<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,我现在这还很遥远,但任何帮助我走向正确方向的帮助,我都在用头撞墙,谢谢。
最佳答案
我不知道你会在哪里替换任何东西,但要找到所有数字,这样的东西就足够了:
// results an array of numbers
var results = $('#text').text().match(/\d+/g);
关于Jquery 查找和替换数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4259030/