| | |
| | | 'use strict'; |
| | | /* eslint-disable regexp/no-empty-capturing-group, regexp/no-empty-group, regexp/no-lazy-ends -- testing */ |
| | | /* eslint-disable regexp/no-useless-quantifier -- testing */ |
| | | var call = require('../internals/function-call'); |
| | | var uncurryThis = require('../internals/function-uncurry-this'); |
| | | var toString = require('../internals/to-string'); |
| | | var regexpFlags = require('../internals/regexp-flags'); |
| | | var stickyHelpers = require('../internals/regexp-sticky-helpers'); |
| | |
| | | var UNSUPPORTED_DOT_ALL = require('../internals/regexp-unsupported-dot-all'); |
| | | var UNSUPPORTED_NCG = require('../internals/regexp-unsupported-ncg'); |
| | | |
| | | var nativeExec = RegExp.prototype.exec; |
| | | var nativeReplace = shared('native-string-replace', String.prototype.replace); |
| | | |
| | | var nativeExec = RegExp.prototype.exec; |
| | | var patchedExec = nativeExec; |
| | | var charAt = uncurryThis(''.charAt); |
| | | var indexOf = uncurryThis(''.indexOf); |
| | | var replace = uncurryThis(''.replace); |
| | | var stringSlice = uncurryThis(''.slice); |
| | | |
| | | var UPDATES_LAST_INDEX_WRONG = (function () { |
| | | var re1 = /a/; |
| | | var re2 = /b*/g; |
| | | nativeExec.call(re1, 'a'); |
| | | nativeExec.call(re2, 'a'); |
| | | call(nativeExec, re1, 'a'); |
| | | call(nativeExec, re2, 'a'); |
| | | return re1.lastIndex !== 0 || re2.lastIndex !== 0; |
| | | })(); |
| | | |
| | | var UNSUPPORTED_Y = stickyHelpers.UNSUPPORTED_Y || stickyHelpers.BROKEN_CARET; |
| | | var UNSUPPORTED_Y = stickyHelpers.BROKEN_CARET; |
| | | |
| | | // nonparticipating capturing group, copied from es5-shim's String#split patch. |
| | | var NPCG_INCLUDED = /()??/.exec('')[1] !== undefined; |
| | |
| | | var PATCH = UPDATES_LAST_INDEX_WRONG || NPCG_INCLUDED || UNSUPPORTED_Y || UNSUPPORTED_DOT_ALL || UNSUPPORTED_NCG; |
| | | |
| | | if (PATCH) { |
| | | // eslint-disable-next-line max-statements -- TODO |
| | | patchedExec = function exec(string) { |
| | | var re = this; |
| | | var state = getInternalState(re); |
| | |
| | | |
| | | if (raw) { |
| | | raw.lastIndex = re.lastIndex; |
| | | result = patchedExec.call(raw, str); |
| | | result = call(patchedExec, raw, str); |
| | | re.lastIndex = raw.lastIndex; |
| | | return result; |
| | | } |
| | | |
| | | var groups = state.groups; |
| | | var sticky = UNSUPPORTED_Y && re.sticky; |
| | | var flags = regexpFlags.call(re); |
| | | var flags = call(regexpFlags, re); |
| | | var source = re.source; |
| | | var charsAdded = 0; |
| | | var strCopy = str; |
| | | |
| | | if (sticky) { |
| | | flags = flags.replace('y', ''); |
| | | if (flags.indexOf('g') === -1) { |
| | | flags = replace(flags, 'y', ''); |
| | | if (indexOf(flags, 'g') === -1) { |
| | | flags += 'g'; |
| | | } |
| | | |
| | | strCopy = str.slice(re.lastIndex); |
| | | strCopy = stringSlice(str, re.lastIndex); |
| | | // Support anchored sticky behavior. |
| | | if (re.lastIndex > 0 && (!re.multiline || re.multiline && str.charAt(re.lastIndex - 1) !== '\n')) { |
| | | if (re.lastIndex > 0 && (!re.multiline || re.multiline && charAt(str, re.lastIndex - 1) !== '\n')) { |
| | | source = '(?: ' + source + ')'; |
| | | strCopy = ' ' + strCopy; |
| | | charsAdded++; |
| | |
| | | } |
| | | if (UPDATES_LAST_INDEX_WRONG) lastIndex = re.lastIndex; |
| | | |
| | | match = nativeExec.call(sticky ? reCopy : re, strCopy); |
| | | match = call(nativeExec, sticky ? reCopy : re, strCopy); |
| | | |
| | | if (sticky) { |
| | | if (match) { |
| | | match.input = match.input.slice(charsAdded); |
| | | match[0] = match[0].slice(charsAdded); |
| | | match.input = stringSlice(match.input, charsAdded); |
| | | match[0] = stringSlice(match[0], charsAdded); |
| | | match.index = re.lastIndex; |
| | | re.lastIndex += match[0].length; |
| | | } else re.lastIndex = 0; |
| | |
| | | if (NPCG_INCLUDED && match && match.length > 1) { |
| | | // Fix browsers whose `exec` methods don't consistently return `undefined` |
| | | // for NPCG, like IE8. NOTE: This doesn' work for /(.?)?/ |
| | | nativeReplace.call(match[0], reCopy, function () { |
| | | call(nativeReplace, match[0], reCopy, function () { |
| | | for (i = 1; i < arguments.length - 2; i++) { |
| | | if (arguments[i] === undefined) match[i] = undefined; |
| | | } |