JS中的正则表达式查找没有3个相同的连续字符 [英] RegEx in JS to find No 3 Identical consecutive characters

查看:44
本文介绍了JS中的正则表达式查找没有3个相同的连续字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找到一个 3 个字符的序列,'abb' 有效而 'abbb' 无效,在 JS 中使用 Regex(可以是字母、数字和非字母数字).

How to find a sequence of 3 characters, 'abb' is valid while 'abbb' is not valid, in JS using Regex (could be alphabets,numerics and non alpha numerics).

这个问题是我在这里提出的问题的变体:如何为 javascript 组合这些正则表达式.

This question is a variation of the question that I have asked in here : How to combine these regex for javascript.

这是错误的: /(^([0-9a-zA-Z]|[^0-9a-zA-Z]))\1\1/ ,那么什么是正确的做法是什么?

This is wrong : /(^([0-9a-zA-Z]|[^0-9a-zA-Z]))\1\1/ , so what is the right way to do it?

推荐答案

这取决于您的实际意思.如果您只想匹配三个不同的字符(即,如果 abb 对您有效),您可以使用这种否定前瞻:

This depends on what you actually mean. If you only want to match three non-identical characters (that is, if abb is valid for you), you can use this negative lookahead:

(?!(.)\1\1).{3}

它首先断言,当前位置后面没有三个相同的字符.然后匹配这三个字符.

It first asserts, that the current position is not followed by three times the same character. Then it matches those three characters.

如果你真的想匹配 3 个不同的字符(只有像 abc 这样的东西),它会变得有点复杂.改用这两个否定前瞻:

If you really want to match 3 different characters (only stuff like abc), it gets a bit more complicated. Use these two negative lookaheads instead:

(.)(?!\1)(.)(?!\1|\2).

首先匹配一个字符.然后我们断言, this 后面没有跟同一个字符.如果是这样,我们匹配另一个字符.然后我们断言这些后面既没有第一个字符也没有第二个字符.然后我们匹配第三个字符.

First match one character. Then we assert, the this is not followed by the same character. If so, we match another character. Then we assert that these are followed neither by the first nor the second character. Then we match a third character.

请注意,那些否定前瞻 ((?!...)) 不消耗任何字符.这就是为什么它们被称为前瞻.他们只是检查接下来会发生什么(或者在这种情况下接下来会发生什么),然后正则表达式从它离开的地方继续.这里是一个很好的教程.

Note that those negative lookaheads ((?!...)) do not consume any characters. That is why they are called lookaheads. They just check what is coming next (or in this case what is not coming next) and then the regex continues from where it left of. Here is a good tutorial.

另请注意,这匹配除换行符以外的任何内容,或者如果您使用 DOTALL 或 SINGLELINE 选项,则匹配任何任何内容.由于您使用的是 JavaScript,您可以通过在正则表达式结束分隔符后附加 s 来激活该选项.如果(出于某种原因)您不想使用此选项,请将 .s 替换为 [\s\S](这始终匹配任何字符).

Note also that this matches anything but line breaks, or really anything if you use the DOTALL or SINGLELINE option. Since you are using JavaScript you can just activate the option by appending s after the regexes closing delimiter. If (for some reason) you don't want to use this option, replace the .s by [\s\S] (this always matches any character).

更新:

在评论中澄清后,我意识到您不想找到三个不同的字符,而是想断言您的字符串>不包含三个相同(且连续)的字符.

After clarification in the comments, I realised that you do not want to find three non-identical characters, but instead you want to assert that your string does not contain three identical (and consecutive) characters.

这更容易一些,并且更接近您之前的问题,因为它只需要一个负面的前瞻.我们所做的是:我们从字符串的开头搜索三个连续的相同字符.但是因为我们想断言这些存在,所以我们将其包装在一个负面的前瞻中:

This is a bit easier, and closer to your former question, since it only requires one negative lookahead. What we do is this: we search the string from the beginning for three consecutive identical characters. But since we want to assert that these do not exist we wrap this in a negative lookahead:

^(?!.*(.)\1\1)

前瞻定位在字符串的开头,所以这是我们唯一要查看的地方.然后,前瞻中的模式会尝试从字符串中的任何位置找到三个相同的字符(因为 .*;相同字符的匹配方式与您之前的问题相同).如果模式找到这些,负前瞻将因此失败,因此字符串将无效.如果找不到三个相同的字符,则内部模式永远不会匹配,因此否定前瞻会成功.

The lookahead is anchored to the beginning of the string, so this is the only place where we will look. The pattern in the lookahead then tries to find three identical characters from any position in the string (because of the .*; the identical characters are matched in the same way as in your previous question). If the pattern finds these, the negative lookahead will thus fail, and so the string will be invalid. If not three identical characters can be found, the inner pattern will never match, so the negative lookahead will succeed.

这篇关于JS中的正则表达式查找没有3个相同的连续字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆