问题描述
您好我正在尝试在JS中使用regEx来识别3个相同的连续字符(可能是字母,数字以及所有非字母数字字符)
Hi I am trying to use regEx in JS for identifying 3 identical consecutive characters (could be alphabets,numbers and also all non alpha numeric characters)
这标识3相同的连续字母和数字:'(([0-9a-zA-Z])\\\)'
这标识了3个相同的连续非字母数字:'(([^ 0-9a -zA-Z])\1\1)'
This identifies 3 identical consecutive alphabets and numbers : '(([0-9a-zA-Z])\1\1)'
This identifies 3 identical consecutive non alphanumerics : '(([^0-9a-zA-Z])\1\1)'
我试图将两者结合起来,如下:'(([0-9a-zA-Z ])\ 1 \ 1)|(([^ 0-9a-zA-Z])\\\)'
I am trying to combine both, like this : '(([0-9a-zA-Z])\1\1)|(([^0-9a-zA-Z])\1\1)'
但我在做错误的东西,它不工作..('88aa3BBdd99 @@'返回true)
But I am doing something wrong and its not working..(returns true for '88aa3BBdd99@@')
编辑:找到3个相同的字符,这似乎是错的/( ^([0-9a-zA-Z] | [^ 0-9a-zA-Z])\1\ 1)/ - >
Edit : And to find NO 3 identical characters, this seems to be wrong /(^([0-9a-zA-Z]|[^0-9a-zA-Z])\1\1)/ --> RegEx in JS to find No 3 Identical consecutive characters
谢谢
Nohsib
thanksNohsib
推荐答案
问题是在整个正则表达式中,反向引用从左到右计算。因此,如果你将它们组合起来,你的数字就会改变:
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
这篇关于如何结合这些正则表达式的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!