var str = '要转化的内容'
function code(str){
var output = str.replace(/&#x([\da-fA-F]+);|&#(\d+);/g, function(match, hexCode,
decCode){
if (hexCode) {
return String.fromCharCode(parseInt(hexCode, 16));
} else {
return String.fromCharCode(parseInt(decCode, 10));
}
});
return output.replace(/<|>|"|&/g, match => {
switch(match) {
case '<':
return '<';
case '>':
return '>';
case '"':
return '"';
case '&':
return '&';
}
})
}
let result=code(str)
console.log("result:" + result)
javaScript,①&#X,&#开头转化成中文,②>;和<;转化成中文
于 2023-06-05 10:58:02 首次发布