FireFox无效正则表达式组 [英] FireFox invalid regex group
问题描述
我有以下正则表达式在 chrome 的问题中正常工作a>,但在FireFox中不起作用,生成语法错误Invalid Regex Group
:
I have the following regex that works fine in chrome but it does not work in FireFox generating syntax error Invalid Regex Group
:
bld = txt.split(/(?<=:)/iu,1);
我尝试使用/(?<=\:)/iu
转义:
,但是它给出了相同的错误.我无法找出造成此问题的原因.
I have tried to escape :
using /(?<=\:)/iu
but it gives the same error. I could not able to figure out the cause of this problem.
推荐答案
您可以看到 ,到目前为止,并非在所有浏览器/JavaScript环境中都支持向后搜索.因此,如果要支持所有浏览器(包括旧版本),则实际上不能依赖该功能.
As you can see here, as of now, lookbehinds are not supported in every browser/JavaScript environment. Thus, you can't actually rely on that feature if you want to support all browsers including legacy versions.
由于您只想匹配除:
和字符串中的第一个:
之外的0+个字符,因此可以使用
Since you just want to match 0+ chars other than :
and the first :
in the string, you may use
s.match(/^[^:]*:/)
请参见 regex演示.
JS:
console.log(
'Error 5: the lorem lipsum: in...'.match(/^[^:]*:/)[0]
);
这篇关于FireFox无效正则表达式组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!