[英]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: 在正则表达式中,插入符号
^
具有两种不同的含义:
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%准确,但就目前而言就足够了。)
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
。
^[^a].*[b-z]a{3,4}$
This expression does the following 该表达式执行以下操作
[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
多次出现。 Live Demo 现场演示
https://regex101.com/r/iF8sN0/3 https://regex101.com/r/iF8sN0/3
Sample text 示范文本
bafaa
bafaaa
bafaaaa
bafaaaaa
Sample Matches 比赛样本
bafaaa
bafaaaa
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"
----------------------------------------------------------------------
^[^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.