javascript 是否有 js 函数用它们的转义序列替换 xml 特殊字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9891151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:08:37  来源:igfitidea点击:

Is there js function that replace xml Special Character with their escape sequence?

javascripthtmlxmlescapingspecial-characters

提问by Dor Cohen

I search the web alot and didn't find js function that replace xml Special Character with their escape sequence?
Is there something like this?

我在网上搜索了很多,但没有找到用转义序列替换 xml 特殊字符的 js 函数?
有这样的吗?

I know about the following:

我知道以下几点:

Special Character   Escape Sequence Purpose  
&                   &           Ampersand sign 
'                   '          Single quote 
"                   "          Double quote
>                   >            Greater than 
<                   &lt;            Less than

is there more? what about writing hexadecimal value like 0×00,
Is this also a problem?

还有更多吗?像0×00这样的十六进制值怎么写,
这也有问题吗?

采纳答案by Barry Kaye

There's an interesting JS library here: Client side HTML encoding and decoding

这里有一个有趣的 JS 库:Client side HTML encoding anddecode

回答by nathanjosiah

I have used this:

我用过这个:

function htmlSpecialChars(unsafe) {
    return unsafe
    .replace(/&/g, "&amp;")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;")
    .replace(/"/g, "&quot;");
}

回答by Alex Turpin

You could use PHP's htmlspecialcharsfrom the PHPJS project.

你可以使用PHP的htmlspecialchars该PHPJS项目

回答by Andrew

This is similar to Can I escape html special chars in javascript?

这类似于我可以在 javascript 中转义 html 特殊字符吗?

The accepted answer there is this:

接受的答案是这样的:

function escapeHtml(unsafe) {
    return unsafe
         .replace(/&/g, "&amp;")
         .replace(/</g, "&lt;")
         .replace(/>/g, "&gt;")
         .replace(/"/g, "&quot;")
         .replace(/'/g, "&#039;");
 }

However, if you're using lodash, then I like cs01's answer from that post:

但是,如果您使用的是 lodash,那么我喜欢该帖子中 cs01 的回答:

_.escape('fred, barney, & pebbles');
// => 'fred, barney, &amp; pebbles'

回答by ADJenks

This is old, but my favorite way to do it is to let the browser figure it out:

这是旧的,但我最喜欢的方法是让浏览器弄清楚:

function encodeXML(data){
    var d = document.createElement('div');
    d.appendChild(document.createTextNode(data));
    return d.innerHTML;
}

encodeXML('&');

Output:

输出:

"&amp;"

“&”

You could also use d.innerText = data;or d.textContent = data;, but createTextNode()has the most browser support.

您也可以使用d.innerText = data;d.textContent = data;,但createTextNode()拥有最多的浏览器支持。

回答by Quentin

Those are the only characters you need to worry about.

这些是您唯一需要担心的字符。

Generally you should be using a DOM interface to build XML documents then you don't need to worry about manually escaping things. That only becomes an issue if you are mashing together strings to build the XML.

通常,您应该使用 DOM 接口来构建 XML 文档,然后您无需担心手动转义。如果您将字符串混搭在一起以构建 XML,那只会成为一个问题。