Skip to content

Commit

Permalink
JS: Better regex detection (#2158)
Browse files Browse the repository at this point in the history
This improves the detection of regular expressions.
  • Loading branch information
RunDevelopment authored Jan 5, 2020
1 parent 3c1fd1c commit a23d8f8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion components/prism-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Prism.languages.javascript['class-name'][0].pattern = /(\b(?:class|interface|ext

Prism.languages.insertBefore('javascript', 'keyword', {
'regex': {
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(?:\[(?:[^\]\\\r\n]|\\.)*]|\\.|[^/\\\[\r\n])+\/[gimyus]{0,6}(?=\s*(?:$|[\r\n,.;})\]]))/,
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(?:\[(?:[^\]\\\r\n]|\\.)*]|\\.|[^/\\\[\r\n])+\/[gimyus]{0,6}(?=(?:\s|\/\*[\s\S]*?\*\/)*(?:$|[\r\n,.;:})\]]|\/\/))/,
lookbehind: true,
greedy: true
},
Expand Down
2 changes: 1 addition & 1 deletion components/prism-javascript.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ Prism.languages.javascript['class-name'][0].pattern = /(\b(?:class|interface|ext

Prism.languages.insertBefore('javascript', 'keyword', {
'regex': {
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(?:\[(?:[^\]\\\r\n]|\\.)*]|\\.|[^/\\\[\r\n])+\/[gimyus]{0,6}(?=\s*(?:$|[\r\n,.;})\]]))/,
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(?:\[(?:[^\]\\\r\n]|\\.)*]|\\.|[^/\\\[\r\n])+\/[gimyus]{0,6}(?=(?:\s|\/\*[\s\S]*?\*\/)*(?:$|[\r\n,.;:})\]]|\/\/))/,
lookbehind: true,
greedy: true
},
Expand Down
19 changes: 18 additions & 1 deletion tests/languages/javascript/regex_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
/ '1' '2' '3' '4' '5' /
[/foo/]

let a = /regex/m // comment
let b = condition ? /regex/ : /another one/

----------------------------------------------------

[
Expand All @@ -21,7 +24,21 @@
["regex", "/[\\]/]/"], ["punctuation", ";"],
["number", "1"], ["operator", "/"], ["number", "4"], ["operator", "+"], ["string", "\"/, not a regex\""], ["punctuation", ";"],
["regex", "/ '1' '2' '3' '4' '5' /"],
["punctuation", "["], ["regex", "/foo/"], ["punctuation", "]"]
["punctuation", "["], ["regex", "/foo/"], ["punctuation", "]"],

["keyword", "let"],
" a ",
["operator", "="],
["regex", "/regex/m"],
["comment", "// comment"],
["keyword", "let"],
" b ",
["operator", "="],
" condition ",
["operator", "?"],
["regex", "/regex/"],
["operator", ":"],
["regex", "/another one/"]
]

----------------------------------------------------
Expand Down

0 comments on commit a23d8f8

Please sign in to comment.