html转义字符有哪些

html-css012

html转义字符有哪些,第1张

HTML的常用的如:<>&"©分别是<,>,&,",©的转义字符

当然还有很多其他的,可以查看对照表解决。

转义的目的就是为了防止冲突或者代码被浏览器执行了。

// 字符串反转义

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值)

}

所有的ASCII码都可以用“\”加数字(一般是8进制数字)来表示。而C中定义了一些字母前加"\"来表示常见的那些不能显示的ASCII字符,如\0,\t,\n等,就称为转义字符,因为后面的字符,都不是它本来的ASCII字符意思了。 所以叫“转义” 转义字符