[JavaScript] Please help me understand this regular expression. Why do we need [^a]? via /r/learnprogramming


[JavaScript] Please help me understand this regular expression. Why do we need [^a]?

//find the regexp to //all the words that finish with a minimum of 3 to a maximum of 4 consecutive "a" //when you run it, the code should only write down "good" messages var threeToFourConsecutiveA = /[^a]+[a]{3,4}$/ 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"); 

We are looking for strings that have 3-4 consecutive a's.

Why does the regexp require [a]? If I understand correctly, this means "not a". Why do we need to search for "not a" + "a{3,4}" ? Rather than just "a{3,4}"?

Thank you for your time. I appreciate your help.

Submitted July 16, 2017 at 07:46PM by NewbiusCoder
via reddit http://ift.tt/2u02jHO

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s