我有以下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>");
答案 0 :(得分:0)
您可能正在寻找以下内容:
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>