[JavaScript] Can someone please explain this replacement to me? via /r/learnprogramming


[JavaScript] Can someone please explain this replacement to me?

console.log( "Hopper, Grace\nMcCarthy, John\nRitchie, Dennis" .replace(/([\w ]+), ([\w ]+)/g, "$2 $1")); // → Grace Hopper // John McCarthy // Dennis Ritchie 

I am confused as to what the "$2" and "$1" is doing.

I thought that the $ was to indicate the end of the match.

Please explain.

Thank you for your time. It is greatly appreciated.

Submitted July 15, 2017 at 12:04AM by NewbiusCoder
via reddit http://ift.tt/2tcEJL9

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