gpt4 book ai didi

javascript - 换行不包含在单词中的字符

转载 作者:行者123 更新时间:2023-11-28 03:00:49 25 4
gpt4 key购买 nike

我目前正在开发一个包含游戏制作者教程的网站。有时我不得不在网站上放几行代码。为了让它看起来更像 Game maker 本身,我想更改文本中特定字符和单词的颜色。

现在我正在尝试更改所有 X 和 Y 的颜色。这是我现在拥有的:

$(document).ready(function(){
$(".code").children().each(function() {
$(this).html($(this).html().replace(/\x\b/g, "<font color=red>x</font>"));
$(this).html($(this).html().replace(/\X\b/g, "<font color=red>X</font>"));
$(this).html($(this).html().replace(/\y\b/g, "<font color=red>y</font>"));
$(this).html($(this).html().replace(/\Y\b/g, "<font color=red>Y</font>"));
});
})

虽然它确实为字符着色,但它也为单词中的字符着色,这不是我想要的。我应该添加或更改什么,以便它只会为不在单词中的 X 和 Y 着色?

最佳答案

我认为这些代码可以解决您的问题。刚刚开始使用 \b 用于正则表达式

$(document).ready(function(){
$(".code").children().each(function() {
$(this).html($(this).html().replace(/\b\x\b/g, "<font color=red>x</font>"));
$(this).html($(this).html().replace(/\b\X\b/g, "<font color=red>X</font>"));
$(this).html($(this).html().replace(/\b\y\b/g, "<font color=red>y</font>"));
$(this).html($(this).html().replace(/\b\Y\b/g, "<font color=red>Y</font>"));
});
});

http://jsfiddle.net/fno9d6wz/

关于javascript - 换行不包含在单词中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34768531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com