Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jquery查找div中的所有数字并添加标签?_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 如何使用jquery查找div中的所有数字并添加标签?

Javascript 如何使用jquery查找div中的所有数字并添加标签?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有一个像这样的DOM <div class="main_div"> <p>A car averages 27 miles per gallon. If gas costs $4.04 per gallon, which of the following is closest to how much the gas would cost for this car to travel 2,727 typical miles?</p> </d

我有一个像这样的DOM

<div class="main_div">
    <p>A car averages 27 miles per gallon. If gas costs $4.04 per gallon, which of the following is closest to how much the gas would cost for this car to travel 2,727 typical miles?</p>    
</div>
我只想在所有数字上添加span标记,如

<div class="main_div">
    <p>A car averages <span> 27 </span> miles per gallon. If gas costs $<span>4.04</span> per gallon, which of the following is closest to how much the gas would cost for this car to travel <span>2,727</span> typical miles?</p>    
</div>
我已经尝试了很多方法,也检查了这些链接


但这只能选择第一个数字,我想选择div中的所有数字,如果div包含20个数字,我只需要添加所有数字,这可能吗?最好的答案必须得到赞赏。

使用正则表达式匹配数字并用

$document.readyfunction{ $'.main\u div p'.eachfunction{ $this.html $this.text.replace/[0-9]+[,][0-9]+|[0-9]+/g,“$1” ; } }; 跨度{ 文字装饰:下划线; } 一辆汽车每加仑平均行驶27英里。如果汽油价格为每加仑4.04美元,以下哪项最接近这辆汽车行驶2727英里的汽油价格?我24岁了,因为24岁

一辆汽车每加仑平均行驶27英里。如果汽油价格为每加仑4.04美元,以下哪项最接近这辆汽车行驶2727英里的汽油价格

如果您害怕弄乱html,请在divs html或文本上使用正则表达式

使用获取匹配项的函数替换正则表达式匹配项。也使处理潜在的边缘情况变得更加容易

var html=jQuerydiv.main_div.html; console.loghtml; html=html.replace/\d*,\d+\d*\。\d+\d+/ig,functionmatch{ 返回+匹配+; }; console.loghtml; jQuerydiv.main_div.htmlhtml 一辆汽车每加仑平均行驶27英里。如果汽油价格为每加仑4.04美元,以下哪项最接近这辆汽车行驶2727英里的汽油价格


@vijayP这是一个数字。作为小数点和千位分隔符,你能告诉我怎么才能找到这是重复的吗,因为我有很多搜索方法,我已经添加了我问题的链接,但我找不到这个?@trincot你能告诉我吗?@JishnuVS:@LinkinTED抱歉,我在问你怎么知道这是一个重复的问题?当我写问题的时候,给我一些建议,我检查了所有的链接,但是我没有得到这个,这就是为什么我问你这个文本的副本。我不明白为什么这会收集选票。@trincot对不起,我的错。这难道不能取代我24岁的年龄吗。我已经24岁了。我老了-这难道不能取代我24岁的年龄吗。我已经24岁了。我老了?会的。我要换正则表达式