使用JavaScript正则表达式将数字HTML实体替换为它们的实际字符
我正在尝试使用JavaScript和regex将数字HTML实体替换为实际的Unicode字符,例如使用JavaScript正则表达式将数字HTML实体替换为它们的实际字符,javascript,regex,replace,entities,html-entities,Javascript,Regex,Replace,Entities,Html Entities,我正在尝试使用JavaScript和regex将数字HTML实体替换为实际的Unicode字符,例如 foo's bar → foo's bar 到目前为止,我得到的是: "foo's bar".replace(/&#([^\s]*);/g, "$1"); // "foo39s bar" 剩下要做的就是用String.fromCharCode($1)替换数字,但我似乎无法让它工作。我该怎么做 "foo's bar".replace(/&
foo's bar
→
foo's bar
到目前为止,我得到的是:
"foo's bar".replace(/&#([^\s]*);/g, "$1"); // "foo39s bar"
剩下要做的就是用String.fromCharCode($1)
替换数字,但我似乎无法让它工作。我该怎么做
"foo's bar".replace(/&#(\d+);/g, function(match, match2) {return String.fromCharCode(+match2);})
在当前示例中,第一个参数(x)是一个“'”。y是39。如果您不想定义所有实体,您可以让浏览器为您定义-此位创建一个空的p元素,写入html并返回它生成的文本。 p元素永远不会添加到文档中
function translateEntities(string){
var text, p=document.createElement('p');
p.innerHTML=string;
text= p.innerText || p.textContent;
p.innerHTML='';
return text;
}
var s= 'foo's bar';
translateEntities(s);
/* returned value: (String)
foo's bar
*/
以及使用回调函数,您可能需要考虑添加对十六进制字符引用的支持(<代码>和x x1234;< /代码>)。 另外,
fromCharCode
可能还不够。例如和#67840
是对腓尼基字符的有效引用,但由于它不在基本的多语言平面内,并且JavaScript的字符串模型基于UTF-16代码单元,而不是完整的字符代码点,fromCharCode(67840)
无法工作。您需要一个UTF-16编码器,例如:
String.fromCharCodePoint= function(/* codepoints */) {
var codeunits= [];
for (var i= 0; i<arguments.length; i++) {
var c= arguments[i];
if (arguments[i]<0x10000) {
codeunits.push(arguments[i]);
} else if (arguments[i]<0x110000) {
c-= 0x10000;
codeunits.push((c>>10 & 0x3FF) + 0xD800);
codeunits.push((c&0x3FF) + 0xDC00);
}
}
return String.fromCharCode.apply(String, codeunits);
};
function decodeCharacterReferences(s) {
return s.replace(/&#(\d+);/g, function(_, n) {;
return String.fromCharCodePoint(parseInt(n, 10));
}).replace(/&#x([0-9a-f]+);/gi, function(_, n) {
return String.fromCharCodePoint(parseInt(n, 16));
});
};
alert(decodeCharacterReferences('Hello 𐤀 mum 𐤀!'));
String.fromCharCodePoint=函数(/*codepoints*/){
var codeunits=[];
对于(var i=0;它只返回“foos bar”
。我缺少什么吗?编辑:哦,显然那是因为匹配=”&
,而不仅仅是39
。请不要这样做。内置HTML解析器拥有太多的权限,无法信任任意内容。这只是在等待XSS的发生。即使设置innerHTML
不会执行脚本元素,但这只是一个向量。还有许多其他向量(CSSexpression
,onerror
处理程序、对象和嵌入元素、嵌入XML和外部实体)列举一些可能导致代码执行或允许任意网络请求的元素。
String.fromCharCodePoint= function(/* codepoints */) {
var codeunits= [];
for (var i= 0; i<arguments.length; i++) {
var c= arguments[i];
if (arguments[i]<0x10000) {
codeunits.push(arguments[i]);
} else if (arguments[i]<0x110000) {
c-= 0x10000;
codeunits.push((c>>10 & 0x3FF) + 0xD800);
codeunits.push((c&0x3FF) + 0xDC00);
}
}
return String.fromCharCode.apply(String, codeunits);
};
function decodeCharacterReferences(s) {
return s.replace(/&#(\d+);/g, function(_, n) {;
return String.fromCharCodePoint(parseInt(n, 10));
}).replace(/&#x([0-9a-f]+);/gi, function(_, n) {
return String.fromCharCodePoint(parseInt(n, 16));
});
};
alert(decodeCharacterReferences('Hello 𐤀 mum 𐤀!'));