且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何结合这些正则表达式的JavaScript

更新时间:2023-02-17 23:40:35

问题是在整个正则表达式中,反向引用从左到右计算。因此,如果你将它们组合起来,你的数字就会改变:

The problem is that backreferences are counted from left to right throughout the whole regex. So if you combine them your numbers change:

(([0-9a-zA-Z])\2\2)|(([^0-9a-zA-Z])\4\4)

你也可以删除外部的parens:

You could also remove the outer parens:

([0-9a-zA-Z])\1\1|([^0-9a-zA-Z])\2\2

或你可以在一组parens中一起捕获替代品,并在结尾附加一个反向引用:

Or you could just capture the alternatives in one set of parens together and append one back-reference to the end:

([0-9a-zA-Z]|[^0-9a-zA-Z])\1\1

但是,既然你的角色类匹配所有字符,你也可以这样:

But since your character classes match all characters anyway you can have that like this as well:

([\s\S])\1\1

如果你激活DOTALL或SINGLELINE选项,您可以使用代替:

And if you activate the DOTALL or SINGLELINE option, you can use a . instead:

(.)\1\1