Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
32 changes: 32 additions & 0 deletions components/prism-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,38 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
Prism.languages.insertBefore('javascript', 'keyword', {
'regex': {
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[[^\]\r\n]+]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,
inside: {
'regex-flag': /[gimyu]{1,5}$/,
'regex-charset': {
pattern: /((?:^|[^\\])(?:\\\\)*)\[(?:\\.|[^\\\]])*\]/,
lookbehind: true,
inside: {
'regex-range': {
pattern: /((?:^|[^\\])(?:\\\\)*)\\?.-\\?./,
lookbehind: true
},
'regex-nageted': {
pattern: /(^\[)\^/,
lookbehind: true
},
// same as the patterns which are not in charset
'regex-escape': /\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|u\{\d+\}|c[A-Za-z]|0[0-7]{1,2}|[^WwDdSsBb\d]|0)/,
'regex-reference': /\\[1-9]/,
// things get different, some characters become normal characters
'regex-charclass': /\\[wds]/i,
'regex-anchor': /\\b/i,
}
},
'regex-escape': /\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|u\{\d+\}|c[A-Za-z]|0[0-7]{1,2}|[^WwDdSsBb\d]|0)/,
'regex-reference': /\\[1-9]/,
'regex-charclass': /(\\[wds])|\./i,
'regex-anchor': /\^|\$|\\b/i,
'regex-quantifier': {
pattern: /\+|\*|(?:\{(?:\d+|\d+,|,\d+|\d+,\d+)\})|(^|[^(])\?/,
lookbehind: true
},
'regex-alternation': /\|/
},
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.

32 changes: 32 additions & 0 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,38 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
Prism.languages.insertBefore('javascript', 'keyword', {
'regex': {
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[[^\]\r\n]+]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,
inside: {
'regex-flag': /[gimyu]{1,5}$/,
'regex-charset': {
pattern: /((?:^|[^\\])(?:\\\\)*)\[(?:\\.|[^\\\]])*\]/,
lookbehind: true,
inside: {
'regex-range': {
pattern: /((?:^|[^\\])(?:\\\\)*)\\?.-\\?./,
lookbehind: true
},
'regex-nageted': {
pattern: /(^\[)\^/,
lookbehind: true
},
// same as the patterns which are not in charset
'regex-escape': /\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|u\{\d+\}|c[A-Za-z]|0[0-7]{1,2}|[^WwDdSsBb\d]|0)/,
'regex-reference': /\\[1-9]/,
// things get different, some characters become normal characters
'regex-charclass': /\\[wds]/i,
'regex-anchor': /\\b/i,
}
},
'regex-escape': /\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|u\{\d+\}|c[A-Za-z]|0[0-7]{1,2}|[^WwDdSsBb\d]|0)/,
'regex-reference': /\\[1-9]/,
'regex-charclass': /(\\[wds])|\./i,
'regex-anchor': /\^|\$|\\b/i,
'regex-quantifier': {
pattern: /\+|\*|(?:\{(?:\d+|\d+,|,\d+|\d+,\d+)\})|(^|[^(])\?/,
lookbehind: true
},
'regex-alternation': /\|/
},
lookbehind: true,
greedy: true
},
Expand Down
102 changes: 92 additions & 10 deletions tests/languages/javascript/regex_feature.test
Original file line number Diff line number Diff line change
@@ -1,25 +1,107 @@
/foo bar/;
/foo/gimyu,
/[\[\]]{2,4}(?:foo)*/;
/[\[\b\]]{2,4}(?:foo)*/;
/foo"test"bar/;
/foo\//
1 / 4 + "/, not a regex";
/ '1' '2' '3' '4' '5' /
[/foo/]
[/foo/],
/\burl\(path\)/,
/gimyu\.jpg$|\.png$/,
/\xCA\xfe\XA1\u4d5a\u{12345}\076\cI?/,
/^.\1+\0?/,
/[s^]\\\[s]\\\\[^sA-c\w\0\1\uffff${1}]/

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

[
["regex", "/foo bar/"], ["punctuation", ";"],
["regex", "/foo/gimyu"], ["punctuation", ","],
["regex", "/[\\[\\]]{2,4}(?:foo)*/"], ["punctuation", ";"],
["regex", "/foo\"test\"bar/"], ["punctuation", ";"],
["regex", "/foo\\//"],
["regex", ["/foo bar/"]], ["punctuation", ";"],
["regex", ["/foo/", ["regex-flag", "gimyu"]]], ["punctuation", ","],
["regex", [
"/",
["regex-charset", [
"[",
["regex-escape", "\\["],
["regex-anchor", "\\b"],
["regex-escape", "\\]"],
"]"
]],
["regex-quantifier", "{2,4}"],
"(?:foo)",
["regex-quantifier", "*"],
"/"
]], ["punctuation", ";"],
["regex", ["/foo\"test\"bar/"]], ["punctuation", ";"],
["regex", ["/foo", ["regex-escape", "\\/"], "/"]],
["number", "1"], ["operator", "/"], ["number", "4"], ["operator", "+"], ["string", "\"/, not a regex\""], ["punctuation", ";"],
["regex", "/ '1' '2' '3' '4' '5' /"],
["punctuation", "["], ["regex", "/foo/"], ["punctuation", "]"]
["regex", ["/ '1' '2' '3' '4' '5' /"]],
["punctuation", "["], ["regex", ["/foo/"]], ["punctuation", "]"], ["punctuation", ","],
["regex", [
"/",
["regex-anchor", "\\b"],
"url",
["regex-escape", "\\("],
"path",
["regex-escape", "\\)"],
"/"
]], ["punctuation", ","],
["regex", [
"/gimyu",
["regex-escape", "\\."],
"jpg",
["regex-anchor", "$"],
["regex-alternation", "|"],
["regex-escape", "\\."],
"png",
["regex-anchor", "$"],
"/"
]], ["punctuation", ","],
["regex", [
"/",
["regex-escape", "\\xCA"],
["regex-escape", "\\xfe"],
["regex-escape", "\\X"],
"A1",
["regex-escape", "\\u4d5a"],
["regex-escape", "\\u{12345}"],
["regex-escape", "\\076"],
["regex-escape", "\\cI"],
["regex-quantifier", "?"],
"/"
]], ["punctuation", ","],
["regex", [
"/",
["regex-anchor", "^"],
["regex-charclass", "."],
["regex-reference", "\\1"],
["regex-quantifier", "+"],
["regex-escape", "\\0"],
["regex-quantifier", "?"],
"/"
]], ["punctuation", ","],
["regex", [
"/",
["regex-charset", ["[s^]"]],
["regex-escape", "\\\\"],
["regex-escape", "\\["],
"s]",
["regex-escape", "\\\\"],
["regex-escape", "\\\\"],
["regex-charset", [
"[",
["regex-nageted", "^"],
"s",
["regex-range", "A-c"],
["regex-charclass", "\\w"],
["regex-escape", "\\0"],
["regex-reference", "\\1"],
["regex-escape", "\\uffff"],
"${1}]"
]],
"/"
]]
]

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

Checks for regex.
Checks for regex.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
["punctuation", ">"]
]],
["script", [
["regex", "/foo/"]
["regex", ["/foo/"]]
]],
["tag", [
["tag", [
Expand All @@ -54,4 +54,4 @@

Checks for scripts containing C# code.
Also checks that those don't break normal JS scripts.
Note: Markup is loaded before Javascript so that scripts are added into grammar.
Note: Markup is loaded before Javascript so that scripts are added into grammar.