html转义字符有哪些

html-css08

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

}

转义字符有很多,在实际编程过程中常常会用到,那么下面介绍一下常用的转义字符。

1、首先打开pycharm,新建一个工程和python文件,如图。

2、打印一段话,输入print添加内容,如下图所示。

3、接着转义字符"\n"换行,如图所示,转义字符"\t"制表符。

4、然后转义字符"\""双引号和"\'"单引号,如下图所示。

5、最后转义字符"\r"回车,如下图所示就完成了。