[JavaScript] I am having trouble understanding this last index concept. via /r/learnprogramming


[JavaScript] I am having trouble understanding this last index concept.

var pattern = /y/g; pattern.lastIndex = 3; var match = pattern.exec("xyzzy"); console.log(match.index); // → 4 console.log(pattern.lastIndex); // → 5 
  1. I believe we are trying to match the for the pattern /y/g. Does g here mean global?

  2. So if lastIndex = 3, we will be starting at the 3 point. Which would be "z"? So we are looking for the "y" after the second "z"? Is this correct?

  3. I do not understand what line 6 is doing.

Thank you for your time. Any help is appreciated.

Submitted July 15, 2017 at 08:26PM by NewbiusCoder
via reddit http://ift.tt/2v58vOT

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