Trouble with replace() and § JavaScript

Trouble with replace() and § JavaScript

本文关键字:#167 JavaScript and with replace Trouble      更新时间:2023-09-26

我试图找出一个问题,当我试图用它的ul代码替换§时,这个问题让我挠头。似乎replace()函数没有选中它。

这里的代码:var replaced = str.replace(/%s/g, "<span style='width:15px;height:10px;'>&#160;</span>").replace(/§/g, "u/00a7")

%s工作正常,所以我认为它的双s本身工作不正常。感谢

我使用的解决方案:最后我无法替换双s,所以最后我使用了&#92;u00a7,它就像一个魅力

"u/00a7"正是JavaScript中的那些字符。你可能是指"'u00a7"。但是用"'u00a7"替换§是没有意义的,它们是同一个字符。如果你想用HTML实体替换它,那就是&#167;:

var s = "See §123";
// Replace
s = s.replace(/§/g, "&#167;");
// Display WITHOUT letting the browser interpret the entity
var p = document.createElement('pre');
if ('textContent' in p) {
  p.textContent = "Not interpreted: " + s;
} else {
  p.innerText = "Not interpreted: " + s;
}
document.body.appendChild(p);
// Display WITH the browser interpreting the entity (so we'll see §)
p = document.createElement('pre');
p.innerHTML = "Interpreted: " + s;
document.body.appendChild(p);

但只要有§就可以了。即使你有字符编码问题(如果你有,你会想解决它们,而不是这样做),§在所有流行的编码中都是同一个字符。。。