[英]Regex: Match if the expression is present, but if the expression have specific string before it don't match
[英]Regex: How to match numbers that don't begin with a specific string
我有以下HTML代码
<pre id="JSCode" class="code">var count1=0; var count2=0;</pre>
我如何更改除以'count'开头的数字以外的所有数字的颜色,我尝试这样做: /[0-9]+/g
但它匹配所有数字?
pre.innerHTML.replace(/[0-9]+/g,"<span style='color: #8be9fd'>$&</span>");
您可能正在寻找以下内容:
window.onload = () => { const element = document.getElementById('JSCode'); element.innerHTML = element.textContent.replace(/(?!(?!count=)\\w+=)([0-9])/g, "<span style='color: #8be9fd'>$1</span>"); }
<pre id="JSCode" class="code">var count1=0; var count2=0;</pre>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.