prism-regex.js 2.3 KB

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