es7.string.match-all.js 1.0 KB

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. // https://tc39.github.io/String.prototype.matchAll/
  3. var $export = require('./_export')
  4. , defined = require('./_defined')
  5. , toLength = require('./_to-length')
  6. , isRegExp = require('./_is-regexp')
  7. , getFlags = require('./_flags')
  8. , RegExpProto = RegExp.prototype;
  9. var $RegExpStringIterator = function(regexp, string){
  10. this._r = regexp;
  11. this._s = string;
  12. };
  13. require('./_iter-create')($RegExpStringIterator, 'RegExp String', function next(){
  14. var match = this._r.exec(this._s);
  15. return {value: match, done: match === null};
  16. });
  17. $export($export.P, 'String', {
  18. matchAll: function matchAll(regexp){
  19. defined(this);
  20. if(!isRegExp(regexp))throw TypeError(regexp + ' is not a regexp!');
  21. var S = String(this)
  22. , flags = 'flags' in RegExpProto ? String(regexp.flags) : getFlags.call(regexp)
  23. , rx = new RegExp(regexp.source, ~flags.indexOf('g') ? flags : 'g' + flags);
  24. rx.lastIndex = toLength(regexp.lastIndex);
  25. return new $RegExpStringIterator(rx, S);
  26. }
  27. });