0

在德国,文本处理器在其智慧中喜欢将低引号放在单词的开头,将大引号放在单词的末尾。因此,您最终会得到一个带有 unicode 8222 代码的文本并运行 .search(String.fromCharCode(8222)) 发现该事件很好,而且 String.fromCharCode(8222) 也很好地显示了该字符。

然而,现在它变得有问题了——用 fromCharCode 找到这个字符,我想下面的代码会用空格替换它:

cSub.replace(/\String.fromCharCode(8222)/g, " ")

但它没有,这也不起作用:

cSub.replace(/String.fromCharCode(8222)/g, " ")

在这两种情况下,字符串都原样返回。我接近编写自己的替换例程,但这不应该是解决方案,我猜?

关于如何用空格替换 8222 个字符的任何建议?

非常感谢

坦率

https://jsfiddle.net/y9cb2e1v/12/试试看。

4

2 回答 2

2

你不能使用这样的正则表达式文字。尝试创建一个RegExp这样的:

const re = RegExp(`[${String.fromCharCode(8222)}${String.fromCharCode(8221)}]`, "g");
console.log(`Your RegExp ${re}`);
console.log(document.querySelector("div").textContent.replace(re, "!"));
<div>"Something &#8222;quoted&#8221;"</div>

于 2021-01-07T10:35:17.753 回答
0

replace通过传递特定值,您可以不使用正则表达式。

function start() {

  var cId = "w3review";

  var cTxt = document.getElementById(cId).value;

  cSub = cTxt.replace(String.fromCharCode(8222), " ");

  var iPos = cTxt.search(String.fromCharCode(8222));

  document.getElementById("txtout").value = "Result: Character found at position " + iPos + ", result from replace: " + cSub;

}
<textarea id="w3review" rows="5" cols="40">
Source Value:
kennen. &bdquo;Neurons that fire
</textarea>

<button onclick="start()">Click me</button>

<textarea id="txtout" rows="5" cols="40">
</textarea>

于 2021-01-07T11:00:43.710 回答