1 | 'use strict';
|
---|
2 | var DESCRIPTORS = require('../internals/descriptors');
|
---|
3 | var globalThis = require('../internals/global-this');
|
---|
4 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
5 | var isForced = require('../internals/is-forced');
|
---|
6 | var inheritIfRequired = require('../internals/inherit-if-required');
|
---|
7 | var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
|
---|
8 | var create = require('../internals/object-create');
|
---|
9 | var getOwnPropertyNames = require('../internals/object-get-own-property-names').f;
|
---|
10 | var isPrototypeOf = require('../internals/object-is-prototype-of');
|
---|
11 | var isRegExp = require('../internals/is-regexp');
|
---|
12 | var toString = require('../internals/to-string');
|
---|
13 | var getRegExpFlags = require('../internals/regexp-get-flags');
|
---|
14 | var stickyHelpers = require('../internals/regexp-sticky-helpers');
|
---|
15 | var proxyAccessor = require('../internals/proxy-accessor');
|
---|
16 | var defineBuiltIn = require('../internals/define-built-in');
|
---|
17 | var fails = require('../internals/fails');
|
---|
18 | var hasOwn = require('../internals/has-own-property');
|
---|
19 | var enforceInternalState = require('../internals/internal-state').enforce;
|
---|
20 | var setSpecies = require('../internals/set-species');
|
---|
21 | var wellKnownSymbol = require('../internals/well-known-symbol');
|
---|
22 | var UNSUPPORTED_DOT_ALL = require('../internals/regexp-unsupported-dot-all');
|
---|
23 | var UNSUPPORTED_NCG = require('../internals/regexp-unsupported-ncg');
|
---|
24 |
|
---|
25 | var MATCH = wellKnownSymbol('match');
|
---|
26 | var NativeRegExp = globalThis.RegExp;
|
---|
27 | var RegExpPrototype = NativeRegExp.prototype;
|
---|
28 | var SyntaxError = globalThis.SyntaxError;
|
---|
29 | var exec = uncurryThis(RegExpPrototype.exec);
|
---|
30 | var charAt = uncurryThis(''.charAt);
|
---|
31 | var replace = uncurryThis(''.replace);
|
---|
32 | var stringIndexOf = uncurryThis(''.indexOf);
|
---|
33 | var stringSlice = uncurryThis(''.slice);
|
---|
34 | // TODO: Use only proper RegExpIdentifierName
|
---|
35 | var IS_NCG = /^\?<[^\s\d!#%&*+<=>@^][^\s!#%&*+<=>@^]*>/;
|
---|
36 | var re1 = /a/g;
|
---|
37 | var re2 = /a/g;
|
---|
38 |
|
---|
39 | // "new" should create a new object, old webkit bug
|
---|
40 | var CORRECT_NEW = new NativeRegExp(re1) !== re1;
|
---|
41 |
|
---|
42 | var MISSED_STICKY = stickyHelpers.MISSED_STICKY;
|
---|
43 | var UNSUPPORTED_Y = stickyHelpers.UNSUPPORTED_Y;
|
---|
44 |
|
---|
45 | var BASE_FORCED = DESCRIPTORS &&
|
---|
46 | (!CORRECT_NEW || MISSED_STICKY || UNSUPPORTED_DOT_ALL || UNSUPPORTED_NCG || fails(function () {
|
---|
47 | re2[MATCH] = false;
|
---|
48 | // RegExp constructor can alter flags and IsRegExp works correct with @@match
|
---|
49 | // eslint-disable-next-line sonarjs/inconsistent-function-call -- required for testing
|
---|
50 | return NativeRegExp(re1) !== re1 || NativeRegExp(re2) === re2 || String(NativeRegExp(re1, 'i')) !== '/a/i';
|
---|
51 | }));
|
---|
52 |
|
---|
53 | var handleDotAll = function (string) {
|
---|
54 | var length = string.length;
|
---|
55 | var index = 0;
|
---|
56 | var result = '';
|
---|
57 | var brackets = false;
|
---|
58 | var chr;
|
---|
59 | for (; index <= length; index++) {
|
---|
60 | chr = charAt(string, index);
|
---|
61 | if (chr === '\\') {
|
---|
62 | result += chr + charAt(string, ++index);
|
---|
63 | continue;
|
---|
64 | }
|
---|
65 | if (!brackets && chr === '.') {
|
---|
66 | result += '[\\s\\S]';
|
---|
67 | } else {
|
---|
68 | if (chr === '[') {
|
---|
69 | brackets = true;
|
---|
70 | } else if (chr === ']') {
|
---|
71 | brackets = false;
|
---|
72 | } result += chr;
|
---|
73 | }
|
---|
74 | } return result;
|
---|
75 | };
|
---|
76 |
|
---|
77 | var handleNCG = function (string) {
|
---|
78 | var length = string.length;
|
---|
79 | var index = 0;
|
---|
80 | var result = '';
|
---|
81 | var named = [];
|
---|
82 | var names = create(null);
|
---|
83 | var brackets = false;
|
---|
84 | var ncg = false;
|
---|
85 | var groupid = 0;
|
---|
86 | var groupname = '';
|
---|
87 | var chr;
|
---|
88 | for (; index <= length; index++) {
|
---|
89 | chr = charAt(string, index);
|
---|
90 | if (chr === '\\') {
|
---|
91 | chr += charAt(string, ++index);
|
---|
92 | } else if (chr === ']') {
|
---|
93 | brackets = false;
|
---|
94 | } else if (!brackets) switch (true) {
|
---|
95 | case chr === '[':
|
---|
96 | brackets = true;
|
---|
97 | break;
|
---|
98 | case chr === '(':
|
---|
99 | result += chr;
|
---|
100 | // ignore non-capturing groups
|
---|
101 | if (stringSlice(string, index + 1, index + 3) === '?:') {
|
---|
102 | continue;
|
---|
103 | }
|
---|
104 | if (exec(IS_NCG, stringSlice(string, index + 1))) {
|
---|
105 | index += 2;
|
---|
106 | ncg = true;
|
---|
107 | }
|
---|
108 | groupid++;
|
---|
109 | continue;
|
---|
110 | case chr === '>' && ncg:
|
---|
111 | if (groupname === '' || hasOwn(names, groupname)) {
|
---|
112 | throw new SyntaxError('Invalid capture group name');
|
---|
113 | }
|
---|
114 | names[groupname] = true;
|
---|
115 | named[named.length] = [groupname, groupid];
|
---|
116 | ncg = false;
|
---|
117 | groupname = '';
|
---|
118 | continue;
|
---|
119 | }
|
---|
120 | if (ncg) groupname += chr;
|
---|
121 | else result += chr;
|
---|
122 | } return [result, named];
|
---|
123 | };
|
---|
124 |
|
---|
125 | // `RegExp` constructor
|
---|
126 | // https://tc39.es/ecma262/#sec-regexp-constructor
|
---|
127 | if (isForced('RegExp', BASE_FORCED)) {
|
---|
128 | var RegExpWrapper = function RegExp(pattern, flags) {
|
---|
129 | var thisIsRegExp = isPrototypeOf(RegExpPrototype, this);
|
---|
130 | var patternIsRegExp = isRegExp(pattern);
|
---|
131 | var flagsAreUndefined = flags === undefined;
|
---|
132 | var groups = [];
|
---|
133 | var rawPattern = pattern;
|
---|
134 | var rawFlags, dotAll, sticky, handled, result, state;
|
---|
135 |
|
---|
136 | if (!thisIsRegExp && patternIsRegExp && flagsAreUndefined && pattern.constructor === RegExpWrapper) {
|
---|
137 | return pattern;
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (patternIsRegExp || isPrototypeOf(RegExpPrototype, pattern)) {
|
---|
141 | pattern = pattern.source;
|
---|
142 | if (flagsAreUndefined) flags = getRegExpFlags(rawPattern);
|
---|
143 | }
|
---|
144 |
|
---|
145 | pattern = pattern === undefined ? '' : toString(pattern);
|
---|
146 | flags = flags === undefined ? '' : toString(flags);
|
---|
147 | rawPattern = pattern;
|
---|
148 |
|
---|
149 | if (UNSUPPORTED_DOT_ALL && 'dotAll' in re1) {
|
---|
150 | dotAll = !!flags && stringIndexOf(flags, 's') > -1;
|
---|
151 | if (dotAll) flags = replace(flags, /s/g, '');
|
---|
152 | }
|
---|
153 |
|
---|
154 | rawFlags = flags;
|
---|
155 |
|
---|
156 | if (MISSED_STICKY && 'sticky' in re1) {
|
---|
157 | sticky = !!flags && stringIndexOf(flags, 'y') > -1;
|
---|
158 | if (sticky && UNSUPPORTED_Y) flags = replace(flags, /y/g, '');
|
---|
159 | }
|
---|
160 |
|
---|
161 | if (UNSUPPORTED_NCG) {
|
---|
162 | handled = handleNCG(pattern);
|
---|
163 | pattern = handled[0];
|
---|
164 | groups = handled[1];
|
---|
165 | }
|
---|
166 |
|
---|
167 | result = inheritIfRequired(NativeRegExp(pattern, flags), thisIsRegExp ? this : RegExpPrototype, RegExpWrapper);
|
---|
168 |
|
---|
169 | if (dotAll || sticky || groups.length) {
|
---|
170 | state = enforceInternalState(result);
|
---|
171 | if (dotAll) {
|
---|
172 | state.dotAll = true;
|
---|
173 | state.raw = RegExpWrapper(handleDotAll(pattern), rawFlags);
|
---|
174 | }
|
---|
175 | if (sticky) state.sticky = true;
|
---|
176 | if (groups.length) state.groups = groups;
|
---|
177 | }
|
---|
178 |
|
---|
179 | if (pattern !== rawPattern) try {
|
---|
180 | // fails in old engines, but we have no alternatives for unsupported regex syntax
|
---|
181 | createNonEnumerableProperty(result, 'source', rawPattern === '' ? '(?:)' : rawPattern);
|
---|
182 | } catch (error) { /* empty */ }
|
---|
183 |
|
---|
184 | return result;
|
---|
185 | };
|
---|
186 |
|
---|
187 | for (var keys = getOwnPropertyNames(NativeRegExp), index = 0; keys.length > index;) {
|
---|
188 | proxyAccessor(RegExpWrapper, NativeRegExp, keys[index++]);
|
---|
189 | }
|
---|
190 |
|
---|
191 | RegExpPrototype.constructor = RegExpWrapper;
|
---|
192 | RegExpWrapper.prototype = RegExpPrototype;
|
---|
193 | defineBuiltIn(globalThis, 'RegExp', RegExpWrapper, { constructor: true });
|
---|
194 | }
|
---|
195 |
|
---|
196 | // https://tc39.es/ecma262/#sec-get-regexp-@@species
|
---|
197 | setSpecies('RegExp');
|
---|