Skip to content

Commit 2a4758a

Browse files
RunDevelopmentmAAdhaTTah
authored andcommitted
Improve regex detection in JS (#1473)
The pattern for regular expressions in JS now also matches empty character sets and it handles expressions like `/[\]/]/` now correctly.
1 parent 81bd8f0 commit 2a4758a

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

components/prism-javascript.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Prism.languages.javascript['class-name'][0].pattern = /(\b(?:class|interface|ext
2323

2424
Prism.languages.insertBefore('javascript', 'keyword', {
2525
'regex': {
26-
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[[^\]\r\n]+]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,
26+
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[(?:[^\]\\\r\n]|\\.)*]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,
2727
lookbehind: true,
2828
greedy: true
2929
},

components/prism-javascript.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prism.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ Prism.languages.javascript['class-name'][0].pattern = /(\b(?:class|interface|ext
738738

739739
Prism.languages.insertBefore('javascript', 'keyword', {
740740
'regex': {
741-
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[[^\]\r\n]+]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,
741+
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[(?:[^\]\\\r\n]|\\.)*]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,
742742
lookbehind: true,
743743
greedy: true
744744
},

tests/languages/javascript/regex_feature.test

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
/foo/gimyu,
33
/[\[\]]{2,4}(?:foo)*/;
44
/foo"test"bar/;
5-
/foo\//
5+
/foo\//;
6+
/[]/;
7+
/[\]/]/;
68
1 / 4 + "/, not a regex";
79
/ '1' '2' '3' '4' '5' /
810
[/foo/]
@@ -14,12 +16,14 @@
1416
["regex", "/foo/gimyu"], ["punctuation", ","],
1517
["regex", "/[\\[\\]]{2,4}(?:foo)*/"], ["punctuation", ";"],
1618
["regex", "/foo\"test\"bar/"], ["punctuation", ";"],
17-
["regex", "/foo\\//"],
19+
["regex", "/foo\\//"], ["punctuation", ";"],
20+
["regex", "/[]/"], ["punctuation", ";"],
21+
["regex", "/[\\]/]/"], ["punctuation", ";"],
1822
["number", "1"], ["operator", "/"], ["number", "4"], ["operator", "+"], ["string", "\"/, not a regex\""], ["punctuation", ";"],
1923
["regex", "/ '1' '2' '3' '4' '5' /"],
2024
["punctuation", "["], ["regex", "/foo/"], ["punctuation", "]"]
2125
]
2226

2327
----------------------------------------------------
2428

25-
Checks for regex.
29+
Checks for regex.

0 commit comments

Comments
 (0)