[JavaScript] Please explain this regular expression to me. I am trying to understand the use of the “+” in this case. via /r/learnprogramming


[JavaScript] Please explain this regular expression to me. I am trying to understand the use of the “+” in this case.

var AtoK5to9AzAndAAt = /^[a-k5-9z@]+$/; console.log(AtoK5to9AzAndAAt.test("az")?"good":"bad"); console.log(AtoK5to9AzAndAAt.test("a#z")?"bad":"good"); console.log(AtoK5to9AzAndAAt.test("@@@@@@@")?"good":"bad"); console.log(AtoK5to9AzAndAAt.test("596@")?"good":"bad"); console.log(AtoK5to9AzAndAAt.test("abc@@992")?"bad":"good"); console.log(AtoK5to9AzAndAAt.test("89@@yaaa%")?"bad":"good"); console.log(AtoK5to9AzAndAAt.test("5k5k5k")?"good":"bad"); console.log(AtoK5to9AzAndAAt.test("aghzn")?"bad":"good"); console.log(AtoK5to9AzAndAAt.test("azzzzzzzzzzzzzz")?"good":"bad"); 

The goal is to return good for all outputs.

However, I am having trouble understanding why the "+" is required.

Isn't the "+" a quantificator which means from one to infinite?

Why do we need the "to infinite" here when we are just trying to find a pattern?

Thank you for your time. Much appreciated.

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

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