es6.regexp.constructor.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var global = require('./_global');
  2. var inheritIfRequired = require('./_inherit-if-required');
  3. var dP = require('./_object-dp').f;
  4. var gOPN = require('./_object-gopn').f;
  5. var isRegExp = require('./_is-regexp');
  6. var $flags = require('./_flags');
  7. var $RegExp = global.RegExp;
  8. var Base = $RegExp;
  9. var proto = $RegExp.prototype;
  10. var re1 = /a/g;
  11. var re2 = /a/g;
  12. // "new" creates a new object, old webkit buggy here
  13. var CORRECT_NEW = new $RegExp(re1) !== re1;
  14. if (require('./_descriptors') && (!CORRECT_NEW || require('./_fails')(function () {
  15. re2[require('./_wks')('match')] = false;
  16. // RegExp constructor can alter flags and IsRegExp works correct with @@match
  17. return $RegExp(re1) != re1 || $RegExp(re2) == re2 || $RegExp(re1, 'i') != '/a/i';
  18. }))) {
  19. $RegExp = function RegExp(p, f) {
  20. var tiRE = this instanceof $RegExp;
  21. var piRE = isRegExp(p);
  22. var fiU = f === undefined;
  23. return !tiRE && piRE && p.constructor === $RegExp && fiU ? p
  24. : inheritIfRequired(CORRECT_NEW
  25. ? new Base(piRE && !fiU ? p.source : p, f)
  26. : Base((piRE = p instanceof $RegExp) ? p.source : p, piRE && fiU ? $flags.call(p) : f)
  27. , tiRE ? this : proto, $RegExp);
  28. };
  29. var proxy = function (key) {
  30. key in $RegExp || dP($RegExp, key, {
  31. configurable: true,
  32. get: function () { return Base[key]; },
  33. set: function (it) { Base[key] = it; }
  34. });
  35. };
  36. for (var keys = gOPN(Base), i = 0; keys.length > i;) proxy(keys[i++]);
  37. proto.constructor = $RegExp;
  38. $RegExp.prototype = proto;
  39. require('./_redefine')(global, 'RegExp', $RegExp);
  40. }
  41. require('./_set-species')('RegExp');