regexp-exec.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. /* eslint-disable regexp/no-empty-capturing-group, regexp/no-empty-group, regexp/no-lazy-ends -- testing */
  3. /* eslint-disable regexp/no-useless-quantifier -- testing */
  4. var call = require('../internals/function-call');
  5. var uncurryThis = require('../internals/function-uncurry-this');
  6. var toString = require('../internals/to-string');
  7. var regexpFlags = require('../internals/regexp-flags');
  8. var stickyHelpers = require('../internals/regexp-sticky-helpers');
  9. var shared = require('../internals/shared');
  10. var create = require('../internals/object-create');
  11. var getInternalState = require('../internals/internal-state').get;
  12. var UNSUPPORTED_DOT_ALL = require('../internals/regexp-unsupported-dot-all');
  13. var UNSUPPORTED_NCG = require('../internals/regexp-unsupported-ncg');
  14. var nativeReplace = shared('native-string-replace', String.prototype.replace);
  15. var nativeExec = RegExp.prototype.exec;
  16. var patchedExec = nativeExec;
  17. var charAt = uncurryThis(''.charAt);
  18. var indexOf = uncurryThis(''.indexOf);
  19. var replace = uncurryThis(''.replace);
  20. var stringSlice = uncurryThis(''.slice);
  21. var UPDATES_LAST_INDEX_WRONG = (function () {
  22. var re1 = /a/;
  23. var re2 = /b*/g;
  24. call(nativeExec, re1, 'a');
  25. call(nativeExec, re2, 'a');
  26. return re1.lastIndex !== 0 || re2.lastIndex !== 0;
  27. })();
  28. var UNSUPPORTED_Y = stickyHelpers.BROKEN_CARET;
  29. // nonparticipating capturing group, copied from es5-shim's String#split patch.
  30. var NPCG_INCLUDED = /()??/.exec('')[1] !== undefined;
  31. var PATCH = UPDATES_LAST_INDEX_WRONG || NPCG_INCLUDED || UNSUPPORTED_Y || UNSUPPORTED_DOT_ALL || UNSUPPORTED_NCG;
  32. if (PATCH) {
  33. patchedExec = function exec(string) {
  34. var re = this;
  35. var state = getInternalState(re);
  36. var str = toString(string);
  37. var raw = state.raw;
  38. var result, reCopy, lastIndex, match, i, object, group;
  39. if (raw) {
  40. raw.lastIndex = re.lastIndex;
  41. result = call(patchedExec, raw, str);
  42. re.lastIndex = raw.lastIndex;
  43. return result;
  44. }
  45. var groups = state.groups;
  46. var sticky = UNSUPPORTED_Y && re.sticky;
  47. var flags = call(regexpFlags, re);
  48. var source = re.source;
  49. var charsAdded = 0;
  50. var strCopy = str;
  51. if (sticky) {
  52. flags = replace(flags, 'y', '');
  53. if (indexOf(flags, 'g') === -1) {
  54. flags += 'g';
  55. }
  56. strCopy = stringSlice(str, re.lastIndex);
  57. // Support anchored sticky behavior.
  58. if (re.lastIndex > 0 && (!re.multiline || re.multiline && charAt(str, re.lastIndex - 1) !== '\n')) {
  59. source = '(?: ' + source + ')';
  60. strCopy = ' ' + strCopy;
  61. charsAdded++;
  62. }
  63. // ^(? + rx + ) is needed, in combination with some str slicing, to
  64. // simulate the 'y' flag.
  65. reCopy = new RegExp('^(?:' + source + ')', flags);
  66. }
  67. if (NPCG_INCLUDED) {
  68. reCopy = new RegExp('^' + source + '$(?!\\s)', flags);
  69. }
  70. if (UPDATES_LAST_INDEX_WRONG) lastIndex = re.lastIndex;
  71. match = call(nativeExec, sticky ? reCopy : re, strCopy);
  72. if (sticky) {
  73. if (match) {
  74. match.input = stringSlice(match.input, charsAdded);
  75. match[0] = stringSlice(match[0], charsAdded);
  76. match.index = re.lastIndex;
  77. re.lastIndex += match[0].length;
  78. } else re.lastIndex = 0;
  79. } else if (UPDATES_LAST_INDEX_WRONG && match) {
  80. re.lastIndex = re.global ? match.index + match[0].length : lastIndex;
  81. }
  82. if (NPCG_INCLUDED && match && match.length > 1) {
  83. // Fix browsers whose `exec` methods don't consistently return `undefined`
  84. // for NPCG, like IE8. NOTE: This doesn't work for /(.?)?/
  85. call(nativeReplace, match[0], reCopy, function () {
  86. for (i = 1; i < arguments.length - 2; i++) {
  87. if (arguments[i] === undefined) match[i] = undefined;
  88. }
  89. });
  90. }
  91. if (match && groups) {
  92. match.groups = object = create(null);
  93. for (i = 0; i < groups.length; i++) {
  94. group = groups[i];
  95. object[group[0]] = match[group[1]];
  96. }
  97. }
  98. return match;
  99. };
  100. }
  101. module.exports = patchedExec;