6

【代码】JS通过正则表达式替换字符串

 1 year ago
source link: https://en.loli.fj.cn/2022/12/11/JS%E9%80%9A%E8%BF%87%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%9B%BF%E6%8D%A2%E5%AD%97%E7%AC%A6%E4%B8%B2/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

【代码】JS通过正则表达式替换字符串

2022-12-11

JS通过正则表达式替换字符串

  • <>"&替换成实体字符
let str = "";

(function(str){
return str.replace(/<|>|"|&/g, function(match) {
switch (match) {
case "<":
return "<";
case ">":
return ">";
case "\"":
return """;
case "&":
return "&";
}
});
})(str);

哔哩哔哩——黑马程序员


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK