UNPKG

2.39 kBJavaScriptView Raw
1(function (Prism) {
2
3 var specialEscape = {
4 pattern: /\\[\\(){}[\]^$+*?|.]/,
5 alias: 'escape'
6 };
7 var escape = /\\(?:x[\da-fA-F]{2}|u[\da-fA-F]{4}|u\{[\da-fA-F]+\}|0[0-7]{0,2}|[123][0-7]{2}|c[a-zA-Z]|.)/;
8 var charSet = {
9 pattern: /\.|\\[wsd]|\\p\{[^{}]+\}/i,
10 alias: 'class-name'
11 };
12 var charSetWithoutDot = {
13 pattern: /\\[wsd]|\\p\{[^{}]+\}/i,
14 alias: 'class-name'
15 };
16
17 var rangeChar = '(?:[^\\\\-]|' + escape.source + ')';
18 var range = RegExp(rangeChar + '-' + rangeChar);
19
20 // the name of a capturing group
21 var groupName = {
22 pattern: /(<|')[^<>']+(?=[>']$)/,
23 lookbehind: true,
24 alias: 'variable'
25 };
26
27 Prism.languages.regex = {
28 'char-class': {
29 pattern: /((?:^|[^\\])(?:\\\\)*)\[(?:[^\\\]]|\\[\s\S])*\]/,
30 lookbehind: true,
31 inside: {
32 'char-class-negation': {
33 pattern: /(^\[)\^/,
34 lookbehind: true,
35 alias: 'operator'
36 },
37 'char-class-punctuation': {
38 pattern: /^\[|\]$/,
39 alias: 'punctuation'
40 },
41 'range': {
42 pattern: range,
43 inside: {
44 'escape': escape,
45 'range-punctuation': {
46 pattern: /-/,
47 alias: 'operator'
48 }
49 }
50 },
51 'special-escape': specialEscape,
52 'char-set': charSetWithoutDot,
53 'escape': escape
54 }
55 },
56 'special-escape': specialEscape,
57 'char-set': charSet,
58 'backreference': [
59 {
60 // a backreference which is not an octal escape
61 pattern: /\\(?![123][0-7]{2})[1-9]/,
62 alias: 'keyword'
63 },
64 {
65 pattern: /\\k<[^<>']+>/,
66 alias: 'keyword',
67 inside: {
68 'group-name': groupName
69 }
70 }
71 ],
72 'anchor': {
73 pattern: /[$^]|\\[ABbGZz]/,
74 alias: 'function'
75 },
76 'escape': escape,
77 'group': [
78 {
79 // https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html
80 // https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference?view=netframework-4.7.2#grouping-constructs
81
82 // (), (?<name>), (?'name'), (?>), (?:), (?=), (?!), (?<=), (?<!), (?is-m), (?i-m:)
83 pattern: /\((?:\?(?:<[^<>']+>|'[^<>']+'|[>:]|<?[=!]|[idmnsuxU]+(?:-[idmnsuxU]+)?:?))?/,
84 alias: 'punctuation',
85 inside: {
86 'group-name': groupName
87 }
88 },
89 {
90 pattern: /\)/,
91 alias: 'punctuation'
92 }
93 ],
94 'quantifier': {
95 pattern: /(?:[+*?]|\{\d+(?:,\d*)?\})[?+]?/,
96 alias: 'number'
97 },
98 'alternation': {
99 pattern: /\|/,
100 alias: 'keyword'
101 }
102 };
103
104}(Prism));