souparno majumder
souparno majumder

Reputation: 2052

what does the following java script expression mean

I was going through a piece of code and I hit against this syntax

str.replace(re,function(raw, p1, p2, p3){
  if (!/\/\//.test(p1)) {  // <---- this one
    //some more code
  }
});

I understand that the test method matches one string with another, and checks if it is present. But what does this regex /\/\// matching the string to? I checked the regex, and

\/ matches the character / literally \/ matches the character / literally

so what does if(!//.test(p1)) doing?

Upvotes: 2

Views: 66

Answers (3)

Batman25663
Batman25663

Reputation: 272

\/ matches the character / literally. The above regex will execute if condition if there are no 2 consecutive /. check out this: here

Upvotes: 1

guest271314
guest271314

Reputation: 1

If first captured group () p1 contains // return false at if condition by converting true to false using ! operator

Upvotes: 1

Borealid
Borealid

Reputation: 98469

The conditional is true if the string does not contain two consecutive slashes.

Upvotes: 5

Related Questions