javascript FireFox 无效的正则表达式组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49816707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:46:41  来源:igfitidea点击:

FireFox invalid regex group

javascriptregexfirefox

提问by SaidbakR

I have the following regex that works fine in chromebut it does not work in FireFox generating syntax error Invalid Regex Group:

我有以下正则表达式在chrome中运行良好,但在 FireFox 中不起作用,生成语法错误Invalid Regex Group

bld = txt.split(/(?<=:)/iu,1);

I have tried to escape :using /(?<=\:)/iubut it gives the same error. I could not able to figure out the cause of this problem.

我试图逃避:使用,/(?<=\:)/iu但它给出了同样的错误。我无法弄清楚这个问题的原因。

采纳答案by Wiktor Stribi?ew

As you can see here, as of now, lookbehinds are only supported in Chrome latest versions. Thus, you can't actually rely on that feature if you want to support all major browsers / older Chrome versions.

正如您在此处看到的,截至目前,仅在 Chrome 最新版本中支持lookbehinds。因此,如果您想支持所有主要浏览器/旧版 Chrome,您实际上不能依赖该功能。

Since you just want to match 0+ chars other than :and the first :in the string, you may use

由于您只想匹配字符串中除:第一个字符以外的 0+ 个字符:,因此您可以使用

s.match(/^[^:]*:/)

See the regex demo.

请参阅正则表达式演示

JS:

JS:

console.log(
 'Error 5: the lorem lipsum: in...'.match(/^[^:]*:/)[0]
);