Commits

Anonymous committed febbbf8

Make JavascriptLexer's regex detection a little smarter (#356)

Comments (0)

Files changed (2)

pygments/lexers/web.py

             (r'<!--', Comment),
             (r'//.*?\n', Comment),
             (r'/\*.*?\*/', Comment),
-            (r'/(\\\\|\\/|[^/\n])*/[gim]*', String.Regex),
+            (r'/(\\\\|\\/|[^/\n])*/[gim]+\b', String.Regex),
+            (r'/(\\\\|\\/|[^/\n])*/(?=\s*[,);])', String.Regex),
+            (r'/(\\\\|\\/|[^/\n])*/(?=\s*\.[a-z])', String.Regex),
             (r'[~\^\*!%&<>\|+=:;,/?\\-]+', Operator),
             (r'[{}\[\]();.]+', Punctuation),
             (r'(for|in|while|do|break|return|continue|if|else|throw|try|'

tests/examplefiles/regex.js

+// regex
+
+blah(/abc/);
+x = /abc/;
+x = /abc/.match;
+
+// math
+
+blah(1/2); //comment
+x = 1 / 2 / 3;
+x = 1/1/.1;
+
+// broken
+
+x=/1/;
+x=1/a/g;
+x=a/a/g;
+
+// real-world
+
+var x = 1/(1+Math.sqrt(sum)); // convert to number between 1-0
+return Math.round((num / den) * 100)/100;