Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中加入这些正则表达式_Javascript - Fatal编程技术网

如何在javascript中加入这些正则表达式

如何在javascript中加入这些正则表达式,javascript,Javascript,如何将以下正则表达式联接到一个表达式中: .replace(/&/g, "&amp;") .replace(/</g, "&lt;") .replace(/>/g, "&gt;") .replace(/"/g, "&quot;") .replace(/'/g, "&#039;"); .replace(/```([\s\S]*?)```/g); var html = $('.content').text().replace(expre

如何将以下正则表达式联接到一个表达式中:

.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
.replace(/```([\s\S]*?)```/g);


var html = $('.content').text().replace(expression, '<code>$1</code>');
是否可以将其全部写入一个正则表达式?

函数替换(str){
常数映射={
“&”:“&;”,
'':'',
'"':'"',
"'":''',
};
返回str.replace(/[&“']|`([\s\s]*?)```/g,x=>map[x]?map[x]:“
”+x.substring(3,x.length-3)+“
”; } 控制台日志(替换为(“”);
console.log(replace(“``second test``');
可能是最适用的。
.replace(/`([\s\s]*?)```/g);
没有替换字符串。您可以尝试此方法;(适用于html实体)您可以将所有正则表达式组合成一个与任何其他正则表达式匹配的表达式:
/[&']].`([\s\s]*?)`/g
,但是你的替代者需要是一个和你的原始代码一样冗长的函数,整个过程变得更难阅读和修改。你原来的代码怎么了?没什么@Paul。很好用。但它看起来有点脏,我在想有没有更好的方法可以得到同样的结果。我可能应该把这个问题转移到代码审查上。非常感谢。