<%response.write(Server.HTMLEncode("The image tag: <img>"))%>
javascript 用正则表达式进行转换处理
var HtmlUtil = {
/*1.用正则表达式实现html转码*/
htmlEncodeByRegExp:function (str){
var s = ""
if(str.length == 0) return ""
s = str.replace(/&/g,"&")
s = s.replace(/</g,"<")
s = s.replace(/>/g,">")
s = s.replace(/ /g," ")
s = s.replace(/\'/g,"'")
s = s.replace(/\"/g,""")
return s
},
/*2.用正则表达式实现html解码*/
htmlDecodeByRegExp:function (str){
var s = ""
if(str.length == 0) return ""
s = str.replace(/&/g,"&")
s = s.replace(/</g,"<")
s = s.replace(/>/g,">")
s = s.replace(/ /g," ")
s = s.replace(/'/g,"\'")
s = s.replace(/"/g,"\"")
return s
}
}
var html = '<div><p data-text-id="0" style="position:absolute">在 这 里 输 入 内 容</p></div>'var text = html.replace(/\s+([^<>]+)(?=<)/g, function (match) { return match.replace(/\s+/g, '&nbsp') })
使用正则表达式即可解决; 例如转义字符\r\n 就只需要如下 *** 作即可: String str = "\r\n"str = str.replaceAll("\r\n","\\r\\n")欢迎分享,转载请注明来源:内存溢出
评论列表(0条)