日期:2014-05-16  浏览次数:20395 次

前台Javascript转换特殊字符
/**
作者信息
name: yeliangbiao
email: ianylb@sina.com

此脚本用来转换特殊字符,如"&"转换成"&"
*/

//在正则表达式中有特殊意义的字符
var regSpecialChars = [ "^","$","(",")","[","]","{","}",".","?","+","*","|","\\"];
//HTML特殊字符集的Map
var specialCharsMap = {"&": "&", //必须放在第一个,先将&转换了,之后就不会影响到转换后生成的&
                    "#":"&#35",//必须放在第二个,先将&转换了,之后就不会影响到转换后生成的&
                    ">" : ">",
                    "<" : "&lt;",
                    "‘":"&lsquo",
                    "’":"&rsquo",
                    ",":"&sbquo",
                    "'":"&#39",
                    "\\":"&#92;",
                    "\"":"&ldquo;",
                    "!":"&#33",
                    "%":"&#37",
                    "~":"&#126",
                    "@":"&#64",
                    "=":"&#61",
                    "(":"&#40",
                    ")":"&#41",
                    "{":"&#123",
                    "}":"&#125",
                    "[":"&#91;",
                    "]":"&#93;",
                    "$":"&#36",
                    "?":"&#63",
                    "*":"&#42;",
                    "|":"&#124;",
                    "^":"&#94;",
                    ".":"&#46;",