[JavaScript] Please help me understand this replacement. via /r/learnprogramming


[JavaScript] Please help me understand this replacement.

function stripComments(code) { return code.replace(/\/\/.*|\/\*[^]*\*\//g, ""); } console.log(stripComments("1 + /* 2 */3")); // → 1 + 3 console.log(stripComments("x = 10;// ten!")); // → x = 10; console.log(stripComments("1 /* a */+/* b */ 1")); // → 1 1 

I am new to this area and I am having difficulty understanding these slashes.

"return code.replace(///.|/\[]\//g, "");"

  1. I understand that patterns are enclosed in forward slashes. But why is there " / \ / \ / "?
  2. I understand that backslashes change the meaning of the pattern, but don't we need to insert characters? Such as +?
  3. Please explain what this pattern is suggesting. I honestly have no idea what I am looking at here.

Thank you for your time.

Submitted July 15, 2017 at 03:34PM by NewbiusCoder
via reddit http://ift.tt/2trLSm1

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