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

Improved javascript regex highlight #1468

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions components/prism-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ 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-flags': {
pattern: /[gimyu]{1,5}$/,
alias: 'keyword'
},
'regex-group': {
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
pattern: /\[(?:\\\\|\\\]|[^\]])*\]/,
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
alias: 'escape'
},
'regex-escape': {
pattern: /\\./,
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
alias: 'escape'
},
'regex-punctuation': {
pattern: /[|^$*+(){}]|(?:\?(?![:!=<]))/,
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
alias: 'operator'
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
}
},
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.

18 changes: 18 additions & 0 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,24 @@ 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-flags': {
pattern: /[gimyu]{1,5}$/,
alias: 'keyword'
},
'regex-group': {
pattern: /\[(?:\\\\|\\\]|[^\]])*\]/,
alias: 'escape'
},
'regex-escape': {
pattern: /\\./,
alias: 'escape'
},
'regex-punctuation': {
pattern: /[|^$*+(){}]|(?:\?(?![:!=<]))/,
alias: 'operator'
}
},
lookbehind: true,
greedy: true
},
Expand Down
43 changes: 34 additions & 9 deletions tests/languages/javascript/regex_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,46 @@
/foo\//
1 / 4 + "/, not a regex";
/ '1' '2' '3' '4' '5' /
[/foo/]
[/foo/],
/url\(path\)/,
/\.jpg$|\.png$/

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

[
["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-flags", "gimyu"]]], ["punctuation", ","],
["regex", [
"/",
["regex-group", "[\\[\\]]"],
["regex-punctuation", "{"],
"2,4",
["regex-punctuation", "}"],
["regex-punctuation", "("],
"?:foo",
["regex-punctuation", ")"],
["regex-punctuation", "*"],
"/"
]], ["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", ["/url", ["regex-escape", "\\("], "path", ["regex-escape", "\\)"], "/"]], ["punctuation", ","],
["regex", [
"/",
["regex-escape", "\\."],
"jpg",
["regex-punctuation", "$"],
["regex-punctuation", "|"],
["regex-escape", "\\."],
"png",
["regex-punctuation", "$"],
"/"
]]
]

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

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.