我有html内容,它包含如此多的元素以及一些货币值 9,999,10,999,24,45,566,1,89,999等我必须在加载页面上使用正则表达式获取这些值。
以下是示例代码,
{{1}}
答案 0 :(得分:0)
检查您的控制台,没有正则表达式。
$('div.poRel h1 > span').each(function(element){
console.log($(this).text());
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="monthRow yrRow cf replacerow">
<div class="desc"><h1>Yearly <span><b>Savings</b> Plan</span></h1></div>
<div class="freeCol poRel">
<h1 class="pepstart">INR<span>9,999</span></h1>
<p class="svng"><!-- Save 26% --></p>
<p class="bldAnvl">Billed Annually</p>
<span class="proM"><p class="opt"><a class="connect premiumBtn mt3" data-form="payment" data-param="2" data-index="19" href="javascript:;">Start Now</a></p></span></div>
<div class="proCol poRel">
<h1 class="peppro">INR<span>14,999</span></h1>
<p class="svng"><!-- Save 26% --></p>
<p class="bldAnvl">Billed Annually</p>
<span class="proM"><p class="opt"><a class="connect premiumBtn" data-form="payment" data-param="3" data-index="19" href="javascript:;">Start Now</a></p></span></div>
<div class="cstClr poRel"><span class="cstM">
<h1 class="pepultima">INR<span>44,999</span></h1>
<p class="svng"><!-- Save 44% --></p>
<p class="bldAnvl">Billed Annually</p>
<p class="opt"><a class="connect premiumBtn" data-form="payment" data-param="4" data-index="19" href="javascript:;">Start Now</a></p></span></div>
</li>
&#13;
答案 1 :(得分:0)
您可以遍历所有跨度并使用/[\d\.]+/g
正则表达式获取数字:
$('span').each(function(element) {
$(this).text().match(/[\d\.]+/g);
});
希望这有帮助。
$('span').each(function(element) {
var num = $(this).text().match(/[\d\.]+/g);
if (num != null) {
var number = num.toString();
console.log(number);
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="monthRow yrRow cf replacerow">
<div class="desc">
<h1>Yearly <span><b>Savings</b> Plan</span></h1>
</div>
<div class="freeCol poRel">
<h1 class="pepstart">INR<span>9,999</span></h1>
<p class="svng">
<!-- Save 26% -->
</p>
<p class="bldAnvl">Billed Annually</p>
<span class="proM"><p class="opt"><a class="connect premiumBtn mt3" data-form="payment" data-param="2" data-index="19" href="javascript:;">Start Now</a></p></span></div>
<div class="proCol poRel">
<h1 class="peppro">INR<span>14,999</span></h1>
<p class="svng">
<!-- Save 26% -->
</p>
<p class="bldAnvl">Billed Annually</p>
<span class="proM"><p class="opt"><a class="connect premiumBtn" data-form="payment" data-param="3" data-index="19" href="javascript:;">Start Now</a></p></span></div>
<div class="cstClr poRel"><span class="cstM">
<h1 class="pepultima">INR<span>44,999</span></h1>
<p class="svng">
<!-- Save 44% -->
</p>
<p class="bldAnvl">Billed Annually</p>
<p class="opt"><a class="connect premiumBtn" data-form="payment" data-param="4" data-index="19" href="javascript:;">Start Now</a></p>
</span>
</div>
</li>
&#13;