7

Is the following code valid?

function test() {
  return /\//.exec("\/");
}
alert(test());

It seems that many javascript minifiers (including jsmin at http://jscompress.com/) consider the "//" on the second line to be the start of a comment. I assume it's a bug in the "trivially" implemented minifiers, since all the browser implementations I tried run it without a problem. Try it on jsfiddle.

7
  • 3
    So this is why some regex engines allow you to specify different delimiters... Commented Jul 21, 2011 at 21:10
  • I'm wondering how the spec states that this doesn't start a comment. Commented Jul 21, 2011 at 21:11
  • 1
    Probably the same way var q = "//"; is valid. Commented Jul 21, 2011 at 21:13
  • 1
    @pimvdb the spec is that, if you have a /, then everything up to the next (non-escaped) / is a regex literal. Commented Jul 21, 2011 at 21:24
  • 2
    That is indeed valid Javascript. I am the owner of jscompress.com and I just deployed a new version of the site built with node.js and UglifyJS that fixes this problem. The past version was PHP, and the PHP minification libraries for JSMin were buggy and without unit tests. Commented Jul 26, 2011 at 17:17

3 Answers 3

3

I was interested in looking it up in the specs, and according to it it is valid:

RegularExpressionLiteral ::
    / RegularExpressionBody / RegularExpressionFlags
RegularExpressionBody ::
    RegularExpressionFirstChar RegularExpressionChars
RegularExpressionChars ::
    [empty]
    RegularExpressionChars RegularExpressionChar
RegularExpressionFirstChar ::
    RegularExpressionNonTerminator but not * or \ or / or [
    RegularExpressionBackslashSequence
    RegularExpressionClass
RegularExpressionChar ::
    RegularExpressionNonTerminator but not \ or / or [
    RegularExpressionBackslashSequence
    RegularExpressionClass
RegularExpressionBackslashSequence ::
    \ RegularExpressionNonTerminator
RegularExpressionNonTerminator ::
    SourceCharacter but not LineTerminator

The \/ is considered a RegularExpressionBackslashSequence, and hence is part of RegularExpressionBody, and as a result cannot be part of the // comment marker.

Sign up to request clarification or add additional context in comments.

Comments

3

Yes, that is valid javascript :) That is a bug in the minifier, and should probably be fixed. You could get around it if you wanted by making your regex have something stupid at the end of it like:

return /\/(?:.|$)/.exec("\/");

Which basically says, either the end of the string, or not the end of the string, without capturing. But I don't think that's a good solution and I wouldn't use it myself haha.

2 Comments

Better: return new RegExp("/").exec("\/");
@PaulPRO .|$ does not match a new line, so it's not an equivalent to the original regexp.
3

Yes, this is legal. \/ matches a literal /. The first \ escapes the /. The line:

/\//.exec("\/");

Evaluates to:

["/"]

1 Comment

As demonstrated in the fiddle :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.