如何使用regex jquery获取currecny格式值

时间:2017-11-21 09:35:29

标签: jquery regex

我有html内容,它包含如此多的元素以及一些货币值 9,999,10,999,24,45,566,1,89,999等我必须在加载页面上使用正则表达式获取这些值。

以下是示例代码,

{{1}}

2 个答案:

答案 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;
&#13;
&#13;

答案 1 :(得分:0)

您可以遍历所有跨度并使用/[\d\.]+/g正则表达式获取数字:

$('span').each(function(element) {
    $(this).text().match(/[\d\.]+/g);
});

希望这有帮助。

&#13;
&#13;
$('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;
&#13;
&#13;