简体   繁体   English

正则表达式:字符串以3到4个连续的“ a”结尾

[英]Regex: string ends with 3 to 4 consecutive “a”

Write a regexp that matches string that finish with 3 to maximally 4 consecutive "a". 编写一个与以3结尾的字符串匹配的regexp, 最多可以匹配4个连续的“ a”。

Tried the following and tests/outputs below. 尝试了以下内容,并在下面进行了测试/输出。 They're supposed to all print "good". 他们应该都打印“好”。 What am I missing here? 我在这里想念什么?

Source: Codecademy 资料来源:Codecademy

//find the regexp to 
//all the words that finish with a minimum of 3 to a maximum of 4 consecutive "a"
var threeToFourConsecutiveA = /^[a]+a{3,4}$/; // THIS IS WHAT I DID

//when you run it, the code should only write down "good" messages 
console.log(threeToFourConsecutiveA.test("baa")?"bad":"good"); 
console.log(threeToFourConsecutiveA.test("baaa")?"good":"bad"); 
console.log(threeToFourConsecutiveA.test("caaaa")?"good":"bad");
console.log(threeToFourConsecutiveA.test("daaaaa")?"bad":"good");
console.log(threeToFourConsecutiveA.test("aaz")?"bad":"good");
console.log(threeToFourConsecutiveA.test("aaaz")?"bad":"good");
console.log(threeToFourConsecutiveA.test("aaaaz")?"bad":"good");
console.log(threeToFourConsecutiveA.test("azazaza")?"bad":"good");

In regular expressions, the caret ^ has two different meanings: 在正则表达式中,插入符号^具有两种不同的含义:

  • Inside the brackets, it means any character that is not from the following . 在方括号内,它表示不是以下字符的任何字符
  • Outside the brackets, it means the string must start here . 在方括号之外,表示字符串必须从此处开始

Compare these two: 比较这两个:

/^[a]+a{3,4}$/ (wrong)
/[^a]+a{3,4}$/ (correct)

(The above explanation is not a 100 percent accurate, as language lawyers will happily point out, but for now it suffices.) (语言律师会很高兴地指出,以上解释并非100%准确,但就目前而言就足够了。)

You used "starts with one or more a" ^[a]+ 你用 “打头的一个或多个” ^[a]+

Solution: (test it here ) 解决方案:( 在此处进行测试)

var reg = /[^a]+a{3,4}$/;

Since [a] means a literal "a" your pattern can't be the good one. 由于[a]表示字面意义上的“ a”,因此您的模式不可能是好的模式。

Try: 尝试:

^(?:[^a]+a+)*[^a]+a?a{3}$

Try this: 尝试这个:

var threeToFourConsecutiveA = /^[^a]+a{3,4}$/;

I think you were missing the second ^ which excludes a . 我以为你失踪了第二^不包括a

Description 描述

^[^a].*[b-z]a{3,4}$

正则表达式可视化

This expression does the following 该表达式执行以下操作

  • validates words that start with anything but an "a", and finish with 3 to 4 consecutive "a". 验证以“ a”以外的任何字符开头并以3到4个连续的“ a”结尾的单词。
  • the [bz] forces a the last characters to be only 3 or 4 a 's. [bz]强制a的最后一个字符仅为3或4 a by using [az] or .* constructs these could allow the letter a to appear multiple times. 通过使用[az].*构造,这些可以允许字母a多次出现。

Example

Live Demo 现场演示

https://regex101.com/r/iF8sN0/3 https://regex101.com/r/iF8sN0/3

Sample text 示范文本

bafaa
bafaaa
bafaaaa
bafaaaaa

Sample Matches 比赛样本

bafaaa
bafaaaa

Explanation 说明

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  [^a]                     any character except: 'a'
----------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  [b-z]                    any character of: 'b' to 'z'
----------------------------------------------------------------------
  a{3,4}                   'a' (between 3 and 4 times (matching the
                           most amount possible))
----------------------------------------------------------------------
  $                        before an optional \n, and the end of a
                           "line"
----------------------------------------------------------------------

Alternative 另类

^[^a][a-z]*a{3}$

正则表达式可视化

Will allow any the last 3 or more characters to be a . 将允许任何过去的3个或更多字符是a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM