replace() 和 § JavaScript 有问题
Trouble with replace() and § JavaScript
我试图找出一个问题,当我尝试用它的 ul 代码替换 § 时,这个问题让我摸不着头脑,似乎 replace() 函数没有选择它。
代码如下:var replaced = str.replace(/%s/g, "<span style='width:15px;height:10px;'> </span>").replace(/§/g, "u/00a7")
%s 工作正常,所以我认为是 double s 本身无法正常工作。谢谢
我使用的解决方案: 最后我无法替换 double s 所以最后我使用了这个:\u00a7
并且它像魅力
"u/00a7"
就是JavaScript中的那些字符。你的意思可能是 "\u00a7"
。但是用 "\u00a7"
替换 §
是没有意义的,它们是同一个字符。如果你打算用 HTML 实体替换它,那就是 §
:
var s = "See §123";
// Replace
s = s.replace(/§/g, "§");
// 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);
...但只是字面上的 § 应该没问题。即使你有字符编码问题(如果你有,你会想解决它们而不是这样做),§ 在所有流行的编码中都是相同的字符...
我试图找出一个问题,当我尝试用它的 ul 代码替换 § 时,这个问题让我摸不着头脑,似乎 replace() 函数没有选择它。
代码如下:var replaced = str.replace(/%s/g, "<span style='width:15px;height:10px;'> </span>").replace(/§/g, "u/00a7")
%s 工作正常,所以我认为是 double s 本身无法正常工作。谢谢
我使用的解决方案: 最后我无法替换 double s 所以最后我使用了这个:\u00a7
并且它像魅力
"u/00a7"
就是JavaScript中的那些字符。你的意思可能是 "\u00a7"
。但是用 "\u00a7"
替换 §
是没有意义的,它们是同一个字符。如果你打算用 HTML 实体替换它,那就是 §
:
var s = "See §123";
// Replace
s = s.replace(/§/g, "§");
// 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);
...但只是字面上的 § 应该没问题。即使你有字符编码问题(如果你有,你会想解决它们而不是这样做),§ 在所有流行的编码中都是相同的字符...