Javascript 更改单个单词';什么颜色?
所有关于谷歌Chrome替换单词的教程都给了我类似的东西-Javascript 更改单个单词';什么颜色?,javascript,google-chrome-extension,Javascript,Google Chrome Extension,所有关于谷歌Chrome替换单词的教程都给了我类似的东西- var replacedText = text.replace(/(hello)/gi, "goodbye"); if (replacedText !== text) { element.style.color = colour; element.replaceChild(document.createTextNode(replacedText), node); } 所以我知道我收到的数据是一个句子,我可以专门替换
var replacedText = text.replace(/(hello)/gi, "goodbye");
if (replacedText !== text) {
element.style.color = colour;
element.replaceChild(document.createTextNode(replacedText), node);
}
所以我知道我收到的数据是一个句子,我可以专门替换单词并更新句子。
我想实现的是把单数词变成不同的颜色。现在它会把整个句子变成红色
有人知道怎么做吗?要上色的文本需要包装在一个跨距中:
this is a test
您需要添加以下内容:
this is a <span style="color:red">test</span>
text.replace(/(\YOUR_WORD)\s/g, "<span style="color:red">$1</span> ")
这是一个测试
在您的条件范围内,您可以执行以下操作:
this is a <span style="color:red">test</span>
text.replace(/(\YOUR_WORD)\s/g, "<span style="color:red">$1</span> ")
text.replace(/(\u-WORD)\s/g,“$1”)
您应该查看并为您的问题添加更多细节,以便其他人可以复制或至少理解您遇到的问题。Related/duplicate:,这需要稍微调整一下,但这是正确的答案。谢谢