Community
Participate
Working Groups
For the following one line program var re = /^[^:/]+:\/\/[^/]+\/?/; JSDT generates the following 5 errors : Description Resource Path Location Type Syntax error on token "Invalid Character", delete this token re.js /re line 1 JavaScript Problem Syntax error on tokens, ArrayLiteralHeader expected instead re.js /re line 1 JavaScript Problem Syntax error on tokens, ArrayLiteralHeader expected instead re.js /re line 1 JavaScript Problem Syntax error on tokens, ArrayLiteralHeader expected instead re.js /re line 1 JavaScript Problem Syntax error on tokens, ArrayLiteralHeader expected instead re.js /re line 1 JavaScript Problem JSLint generates the much more understandable: Error: Problem at line 1 character 30: Unescaped '/'. var re = /^[^:/]+:\/\/[^/]+\/?/; Problem at line 1 character 40: Unescaped '/'. var re = /^[^:/]+:\/\/[^/]+\/?/; Adding backslashes before the two internal forward slashes make both parsers happy. See also https://github.com/jquery/jquery-mobile/issues/issue/1010
I notice in the current 3.6, we don't generate any errors for this, even though JSlint does. Possible that bug404832 over corrected the error messages seen there.
I can confirm that fix for the bug #404832 also fixes this issue.
My concern is that we seem to no longer being showing an actual error as identified by JSLint.
Chris, do you see some error in that RegEx?
If you run it though JSLint, you see errors, as the submitter stated.
Chris, JSLint shows errors even for a 'good' pattern /^[^:\/]+:\/\/[^\/]+\/?/ (taken from https://github.com/jquery/jquery-mobile/commit/1c9414d0306d415f953ce3695fecba186ff6178f). Java Pattern.compile() say no errors on both patterns ("bad" /^[^:/]+:\/\/[^/]+\/?/ and "good" /^[^:\/]+:\/\/[^\/]+\/?/) and both patterns work well with Patern.compile. Also, the following script block also works well in my FF and there are no errors in Web Console: <script type="text/javascript"> var ua = "http://www.google.com/somefolder/somefile.html"; var ur = "/somefolder/somefile.html"; var pb = /^[^:/]+:\/\/[^/]+\/?/; var pg = /^[^:\/]+:\/\/[^\/]+\/?/; var mab = ua.search(pb); var mag = ua.search(pg); var mrb = ur.search(pb); var mrg = ur.search(pg); window.alert("Search in '" + ua + "' by 'bad' pattern /^[^:/]+:\/\/[^/]+\/?/ == " + mab + "\n" + "Search in '" + ua + "' by 'good' pattern /^[^:\/]+:\/\/[^\/]+\/?/ == " + mag + "\n" + "Search in '" + ur + "' by 'bad' pattern /^[^:/]+:\/\/[^/]+\/?/ == " + mrb + "\n" + "Search in '" + ur + "' by 'good' pattern /^[^:\/]+:\/\/[^\/]+\/?/ == " + mrg); </script> So, probably both RegExps are valid. Looks like jQuery guys just made a workaround to bug #404832 in their code, but not their own error fixing.
I cannot find any restrictions on using unescaped '/'-character in ClassRanges (between '[' and ']') in "ECMAScript Language Specification ECMA-262, 5.1 Edition, June 2011" (clause 15.10). "15.10.2.18 ClassAtomNoDash The production ClassAtomNoDash :: SourceCharacter but not one of \ or ] or - evaluates by returning a one-element CharSet containing the character represented by SourceCharacter." So, the only \ ] and - characters are to be escaped inside a ClassRange. All other non-whitespace chaacters are allowed to be unescaped including forward slash (/) character. Escaped forward character (\/) means the same as the forward character itself (/) - IMHO, that's why my test script in FF works well and Pattern.compile() works well for both escaped and unescaped variations of the pattern.