Skip to content

Commit

Permalink
Improve JS regex detections
Browse files Browse the repository at this point in the history
  • Loading branch information
RexSkz committed Jul 15, 2018
1 parent 7271bf2 commit fbb7100
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 40 deletions.
30 changes: 17 additions & 13 deletions components/prism-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,26 @@ 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-flag': {
pattern: /(\/)[gimyu]{1,5}$/,
lookbehind: true
},
'regex-group': {
pattern: /\[(?:\\\\|\\\]|[^\]])*\]/,
alias: 'escape'
'regex-charset1': {
pattern: /(\[(\\\]|[^\]])*\])|(\\[wds])/i,
alias: 'regex-charset'
},
'regex-escape': {
pattern: /\\./,
alias: 'escape'
'regex-escape': /\\(\\|\+|x[0-9a-f]{2}|u[0-9A-F]{4}|u\{\d+\}|c[a-z]|0[0-7]{1,2}|[^\d])/i,
'regex-reference': /\\\d/,
'regex-charset2': {
pattern: /\./,
alias: 'regex-charset'
},
'regex-punctuation': {
pattern: /[|^$*+(){}]|(?:\?(?![:!=<]))/,
alias: 'operator'
}
'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.

30 changes: 17 additions & 13 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,22 +733,26 @@ 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-flag': {
pattern: /(\/)[gimyu]{1,5}$/,
lookbehind: true
},
'regex-group': {
pattern: /\[(?:\\\\|\\\]|[^\]])*\]/,
alias: 'escape'
'regex-charset1': {
pattern: /(\[(\\\]|[^\]])*\])|(\\[wds])/i,
alias: 'regex-charset'
},
'regex-escape': {
pattern: /\\./,
alias: 'escape'
'regex-escape': /\\(\\|\+|x[0-9a-f]{2}|u[0-9A-F]{4}|u\{\d+\}|c[a-z]|0[0-7]{1,2}|[^\d])/i,
'regex-reference': /\\\d/,
'regex-charset2': {
pattern: /\./,
alias: 'regex-charset'
},
'regex-punctuation': {
pattern: /[|^$*+(){}]|(?:\?(?![:!=<]))/,
alias: 'operator'
}
'regex-anchor': /(\^|\$|\\b)/i,
'regex-quantifier': {
pattern: /\+|\*|(?:\{(?:\d+|\d+,|,\d+|\d+,\d+)\})|(^|[^(])\?/,
lookbehind: true
},
'regex-alternation': /\|/
},
lookbehind: true,
greedy: true
Expand Down
44 changes: 31 additions & 13 deletions tests/languages/javascript/regex_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@
/ '1' '2' '3' '4' '5' /
[/foo/],
/url\(path\)/,
/\.jpg$|\.png$/
/\.jpg$|\.png$/,
/\xCA\xfe\u4d5a\u{12345}\076\cI?/,
/^.\1+?/

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

[
["regex", ["/foo bar/"]], ["punctuation", ";"],
["regex", ["/foo/", ["regex-flags", "gimyu"]]], ["punctuation", ","],
["regex", ["/foo/", ["regex-flag", "gimyu"]]], ["punctuation", ","],
["regex", [
"/",
["regex-group", "[\\[\\]]"],
["regex-punctuation", "{"],
"2,4",
["regex-punctuation", "}"],
["regex-punctuation", "("],
"?:foo",
["regex-punctuation", ")"],
["regex-punctuation", "*"],
["regex-charset1", "[\\[\\]]"],
["regex-quantifier", "{2,4}"],
"(?:foo)",
["regex-quantifier", "*"],
"/"
]], ["punctuation", ";"],
["regex", ["/foo\"test\"bar/"]], ["punctuation", ";"],
Expand All @@ -36,11 +34,31 @@
"/",
["regex-escape", "\\."],
"jpg",
["regex-punctuation", "$"],
["regex-punctuation", "|"],
["regex-anchor", "$"],
["regex-alternation", "|"],
["regex-escape", "\\."],
"png",
["regex-punctuation", "$"],
["regex-anchor", "$"],
"/"
]], ["punctuation", ","],
["regex", [
"/",
["regex-escape", "\\xCA"],
["regex-escape", "\\xfe"],
["regex-escape", "\\u4d5a"],
["regex-escape", "\\u{12345}"],
["regex-escape", "\\076"],
["regex-escape", "\\cI"],
["regex-quantifier", "?"],
"/"
]], ["punctuation", ","],
["regex", [
"/",
["regex-anchor", "^"],
["regex-charset2", "."],
["regex-reference", "\\1"],
["regex-quantifier", "+"],
["regex-quantifier", "?"],
"/"
]]
]
Expand Down

0 comments on commit fbb7100

Please sign in to comment.