我有一个正则表达式如下:
var re = new RegExp('(?<!\r)\n', 'g');
在 Chrome 中运行良好,
但在 Firefox 中出现以下错误:
语法错误:无效的正则表达式组
它也适用于 Node.js
最佳答案
您可以使用 try/catch 表达式
try {
var re = new RegExp('(?<!\r)\n', 'g');
}
catch() {
var firefox = true;
//add alternate RegExp
}
您还可以检查其用户代理:
if (navigator.userAgent.indexOf("Chrome") !== -1) {
//Code that works on chrome
} else {
//code for firefox
}
这是因为 chrome 支持look-behind 表达式,而 firefox 不支持。
来源:https://www.w3schools.com/和 https://developer.mozilla.org/
关于javascript - 正则表达式在 Chrome 中有效,但在 Firefox 中出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59992288/