Skip to content

Commit

Permalink
Improve regex detection in JS (#1473)
Browse files Browse the repository at this point in the history
The pattern for regular expressions in JS now also matches empty character
sets and it handles expressions like `/[\]/]/` now correctly.
  • Loading branch information
RunDevelopment authored and mAAdhaTTah committed Dec 1, 2018
1 parent 81bd8f0 commit 2a4758a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion components/prism-javascript.js
Expand Up @@ -23,7 +23,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])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[(?:[^\]\\\r\n]|\\.)*]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\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
Expand Up @@ -738,7 +738,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])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[(?:[^\]\\\r\n]|\\.)*]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,
lookbehind: true,
greedy: true
},
Expand Down
10 changes: 7 additions & 3 deletions tests/languages/javascript/regex_feature.test
Expand Up @@ -2,7 +2,9 @@
/foo/gimyu,
/[\[\]]{2,4}(?:foo)*/;
/foo"test"bar/;
/foo\//
/foo\//;
/[]/;
/[\]/]/;
1 / 4 + "/, not a regex";
/ '1' '2' '3' '4' '5' /
[/foo/]
Expand All @@ -14,12 +16,14 @@
["regex", "/foo/gimyu"], ["punctuation", ","],
["regex", "/[\\[\\]]{2,4}(?:foo)*/"], ["punctuation", ";"],
["regex", "/foo\"test\"bar/"], ["punctuation", ";"],
["regex", "/foo\\//"],
["regex", "/foo\\//"], ["punctuation", ";"],
["regex", "/[]/"], ["punctuation", ";"],
["regex", "/[\\]/]/"], ["punctuation", ";"],
["number", "1"], ["operator", "/"], ["number", "4"], ["operator", "+"], ["string", "\"/, not a regex\""], ["punctuation", ";"],
["regex", "/ '1' '2' '3' '4' '5' /"],
["punctuation", "["], ["regex", "/foo/"], ["punctuation", "]"]
]

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

Checks for regex.
Checks for regex.

0 comments on commit 2a4758a

Please sign in to comment.