Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaScript正则表达式将数字HTML实体替换为它们的实际字符_Javascript_Regex_Replace_Entities_Html Entities - Fatal编程技术网

使用JavaScript正则表达式将数字HTML实体替换为它们的实际字符

使用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(/&

我正在尝试使用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(/&#(\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 &#x10900; mum &#67840;!'));
String.fromCharCodePoint=函数(/*codepoints*/){
var codeunits=[];

对于(var i=0;它只返回
“foos bar”
。我缺少什么吗?编辑:哦,显然那是因为
匹配
=
”&
,而不仅仅是
39
。请不要这样做。内置HTML解析器拥有太多的权限,无法信任任意内容。这只是在等待XSS的发生。即使设置
innerHTML
不会执行脚本元素,但这只是一个向量。还有许多其他向量(CSS
expression
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 &#x10900; mum &#67840;!'));