使用正则表达式(regex)替换jQuery/JavaScript中的选定文本

问题描述:

在下面的示例中,使用jQuery选择文本.我们如何通过摆脱其他数据来隔离货币?

In the example below, the text is selected using jQuery. How can we isolate the currency by getting rid of the other data?

这种使用JavaScript的replace的尝试无效:

This attempt at using JavaScript's replace did not work:

var symbol = $("div.price > h5 > div.num").text().replace(/[\d.]*/, "");

这是示例HTML; jQuery选择器正在工作:

This is the example HTML; the jQuery selector is working:

<div class="price">
  <h5 class="biguns">
    <div class="num">
      €12.28
    </div>
    Lowest Price Per Night
  </h5>
</div>

必须另点转义点,使其与每个字符匹配,并且必须设置全局修饰符:

The dot must be escaped othwerwise it will match every character and you must set the global modifier:

var symbol = $("div.price > h5 > div.num").text().replace(/[\d\.]+/g, "");