简体   繁体   English

如何使用javascript转换特殊字符及其实体名称(如&与&)

[英]How to Convert special chars with their entity names (like & with &) using javascript

I have small requirement, have JSON data and I would like to convert all specialChars with escapeChars , I tried with following code but it throws an error like Object doesn't support property or method 'split' , please share me your ideas. 我的要求不高,有JSON数据,我想将所有specialChars转换为escapeChars ,我尝试使用以下代码,但会引发错误,例如Object doesn't support property or method 'split' ,请与我分享您的想法。

function applyEscapeChars()
                {                              
                            var str = { "aaData": ["dummy < data","ampersand&aaa","2015-02-16","2015-02-16",470.5,"xyz",1,0,"","Error: 1 Invalid format/sample","---"]}                            
                            var specialChars = new Array();
                                specialChars[0] = "&";
                                specialChars[1] = "\"";
                                specialChars[2] = "'";
                                specialChars[3] = "<";
                                specialChars[4] = ">";
                                var escapeChars = new Array();
                                escapeChars[0] = "&amp;";
                                escapeChars[1] = "&quot;";
                                escapeChars[2] = "&apos;";
                                escapeChars[3] = "&lt;";
                                escapeChars[4] = "&gt;";

                                for (var i =0; i < specialChars.length; i++ )
                                {                             
                                         str = JSON.stringify(str ).split(specialChars[i]).join(escapeChars[i]);                                             
                                }
                                //alert(str);

                                return str;

                }

Now everything is working fine except less than symbol(<) 现在一切正常,但less than symbol(<)

Is it ok for you to let the browser do the html encoding? 您可以让浏览器进行html编码吗?

function htmlEncode(value){
  //create a in-memory div, set it's inner text(which jQuery automatically encodes)
  //then grab the encoded contents back out.  The div never exists on the page.
  return $('<div/>').text(value).html();
}

It's taken from HTML-encoding lost when attribute read from input field 从输入字段读取属性时丢失的HTML编码中获取

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 javascript 将特殊的 UTF-8 字符转换为其等效的 iso-8859-1? - How do I convert special UTF-8 chars to their iso-8859-1 equivalent using javascript? 如何使用JavaScript在警报框中显示特殊字符? - How can I display special chars in an alert box using javascript? 使用Javascript正则表达式“&”将字符串替换为“&”,而忽略诸如“©”之类的特殊实体 - String replace using Javascript regex “&” to “&amp;” while ignoring a Special Entities like “&#169;” 如何使用 javascript 将每个特殊字符和表情符号转换为其 html 实体? - How can i convert every special character and emoji into its html entity using javascript? 如何从Javascript中的字符串替换这个特殊字符 - how to replace this special chars from a string in Javascript 验证特殊字符JavaScript - Validate special chars JavaScript JavaScript替换特殊字符{}“ - Javascript replace special chars { } " 如何使用jquery或javascript只允许文本框中的英文,数字和特殊字符? - How to allow only english,numeric and special chars in textbox using jquery or javascript? 如何判断字符串是否包含HTML实体(如&)? - How to tell if a string contains HTML entity (like &amp;)? 使用Javascript从外部文件读取UTF-8特殊字符 - Read UTF-8 special chars from external file using Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM