public static String toHTMLString(String in) {
StringBuffer out = new StringBuffer()
for (int i = 0in != null &&i <in.length()i++) {
char c = in.charAt(i)
if (c == '\'')
out.append("'")
else if (c == '\"')
out.append(""")
else if (c == '<')
out.append("<")
else if (c == '>')
out.append(">")
else if (c == '&')
out.append("&")
else if (c == ' ')
out.append(" ")
else if (c == '\n')
out.append("<br/>")
else
out.append(c)
}
return out.toString()
}
// 字符串反转义
function enXssHtml(text) {
const matchList = {
"<": "<",
">": ">",
"&": "&",
"": '""',
""": '"',
"'": "'",
}
let regStr = "(" + Object.keys(matchList).toString() + ")"
// ↑ ------------【 提取匹配列表key值 】.【组数转字符串】
regStr = regStr.replace(/,/g, ")|(")
// ↑ 通过匹配将其更新为正则的字符串类型
const regExp = new RegExp(regStr, "g")
// ↑ ------- 字符串 转 正则 方法
return text.replace(regExp, (match) =>matchList[match])
// ↑ ------ 替换方法 (正则, 当前key =>返回当前被匹配的key值)
}
// 字符串反转义
function deXssHtml(text) {
const matchList = {
"<": "<",
">": ">",
"&": "&",
'"': '"',
'"': '"',
"'": "'",
}
let regStr = "(" + Object.keys(matchList).toString() + ")"
// ↑ ------------【 提取匹配列表key值 】.【组数转字符串】
regStr = regStr.replace(/,/g, ")|(")
// ↑ 通过匹配将其更新为正则的字符串类型
const regExp = new RegExp(regStr, "g")
// ↑ ------- 字符串 转 正则 方法
return text.replace(regExp, (match) =>matchList[match])
// ↑ ------ 替换方法 (正则, 当前key =>返回当前被匹配的key值)
}