Javascript查找和替换代码需要更正

时间:2018-01-23 09:50:47

标签: javascript replace syntax-error textnode

以下代码(包含在网站标题中)包含错误。我认为代码的意图是明确的,但它包含语法和/或其他错误?请更正。

编辑2:完全最小的例子(js小提琴:https://jsfiddle.net/g1u2p36k/):

<html>

<head>
<script language="javascript">
function walkText(node) {
  if (node.nodeType == 3) {
    node.data = node.data.replace(/"Ashen Glow Gaming"/gi, "<span style=\"font-color: red;\">a<span style=\"color: blue\">shen</span> g<span style=\"color: blue\">low</span> g<span style=\"color: blue\">aming</span></span>");
  }
  if (node.nodeType == 1 && node.nodeName != "SCRIPT") {
    for (var i = 0; i < node.childNodes.length; i++) {
      walkText(node.childNodes[i]);
    }
  }
}
walkText(document.body);
</script>
</head>

<body>
blah blah Ashen Glow Gaming <a href="https://www.ashenglowgaming.com" class="Ashen Glow Gaming">an ashen glow gaming link</a> blah blah
</body>
</html>

这应该导致字符串的所有实例&#34; Ashen Glow Gaming&#34;被替换为文本中特殊格式的版本:

enter image description here

修改:JSLint确定了2个Expected '\s' and instead saw ' '.个实例和一个Expected '/'.个实例:

enter image description here

1 个答案:

答案 0 :(得分:1)

没有转义引号中的引号。

node.data = node.data.replace(/"Ashen Glow Gaming"/gi, "<span style=\"font-family: elektora;\">a<span style=\"font-size: 80%\">shen</span> g<span style=\"font-size: 80%\">low</span> g<span style=\"font-size: 80%\">aming</span></span>");
相关问题