[英]Javascript to strip html and currency symbol
I have this html: 我有这个HTML:
<span class="price-amount amount"><span class="price-currencySymbol">£</span>49.99</span>
I want to extract just the value of '49.99' without the html and currency symbol. 我只想提取'49 .99'的值而没有html和货币符号。
I have tried this: 我已经试过了:
function() {
var element = document.querySelector('.price-amount');
var price = element.innerHTML.replace('£', '');
return price;
}
But the result is this: 但是结果是这样的:
<span class="price-currencySymbol">£</span>49.99
Rather than: 49.99 而不是:49.99
By using selector logic and accessing the proper text node you don't even need to use regexp. 通过使用选择器逻辑并访问适当的文本节点,您甚至不需要使用regexp。
var price = document.querySelector(".price-amount") .childNodes[1].textContent; console.log(`Extracted price: ${price}`);
<span class="price-amount amount"><span class="price-currencySymbol">£</span>49.99</span>
如果您想在每种情况下都删除'.price-currencySymbol'
跨度中的内容,则只需清空其html:
document.querySelector(".price-amount").innerHTML = "";
Try this 尝试这个
var price = element.innerHTML.match(/\d+\.\d+/)[0];
where 哪里
.match
searches the pattern of price using regex. .match
使用正则表达式搜索价格模式。
[0]
returns the first match, and in your case it would be the only match. [0]
返回第一个匹配项,在您的情况下,它将是唯一的匹配项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.