Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS: Better regex detection #2158

Merged
merged 2 commits into from
Jan 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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