Skip to content

Commit be5f3db

Browse files
author
胡子豪
authored
Update 07.3.md
fix 通过正则修改内容中的代码未处理HTML转义的问题
1 parent 72d959b commit be5f3db

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

zh/07.3.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,29 @@ func main() {
7575
src := string(body)
7676

7777
//将HTML标签全转换成小写
78-
re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
78+
re, _ := regexp.Compile(`<[\S\s]+?>`)
7979
src = re.ReplaceAllStringFunc(src, strings.ToLower)
8080

8181
//去除STYLE
82-
re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>")
82+
re, _ = regexp.Compile(`<style[\S\s]+?</style>`)
83+
src = re.ReplaceAllString(src, "")
84+
//去除HTMLUnscape的STYLE
85+
re, _ = regexp.Compile(`&lt;style[\S\s]+?&lt;/style&gt;`)
8386
src = re.ReplaceAllString(src, "")
8487

8588
//去除SCRIPT
86-
re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>")
89+
re, _ = regexp.Compile(`<script[\S\s]+?</script>`)
90+
src = re.ReplaceAllString(src, "")
91+
//去除HTMLUnsapce的SCRIPT
92+
re, _ = regexp.Compile(`&lt;script[\S\s]+?&lt;/script&gt;`)
8793
src = re.ReplaceAllString(src, "")
8894

8995
//去除所有尖括号内的HTML代码,并换成换行符
90-
re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
96+
re, _ = regexp.Compile(`<[\S\s]+?>`)
9197
src = re.ReplaceAllString(src, "\n")
9298

9399
//去除连续的换行符
94-
re, _ = regexp.Compile("\\s{2,}")
100+
re, _ = regexp.Compile(`\s{2,}`)
95101
src = re.ReplaceAllString(src, "\n")
96102

97103
fmt.Println(strings.TrimSpace(src))

0 commit comments

Comments
 (0)