A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://gist.github.com/rauschma/6330265 below:

A version of RegExp.prototype.exec() that returns an iterable · GitHub

Last active April 7, 2019 12:09

Clone this repository at <script src="https://gist.github.com/rauschma/6330265.js"></script>

Save rauschma/6330265 to your computer and use it in GitHub Desktop.

A version of RegExp.prototype.exec() that returns an iterable

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters console.log(extractQuotedTextES5('“a” and “b” or “c”')); // [ 'a', 'b', 'c' ] // If exec() is invoked on a regular expression whose flag /g is set // then the regular expression is abused as an iterator: // Its property `lastIndex` tracks how far along the iteration is // and must be reset. It also means that the regular expression can’t be frozen. var regexES5 = /(.*?)/g; function extractQuotedTextES5(str) { regexES5.lastIndex = 0; // to be safe var results = []; var match; while (match = regexES5.exec(str)) { results.push(match[1]); } return results; } // If we had a method `execAll()` that returns an iterable, // the above code would become simpler in ES6. const REGEX_ES6 = /(.*?)/; // no need to set flag /g function extractQuotedTextES6a(str) { let results = []; for (let match of REGEX_ES6.execAll(str)) { results.push(match[1]); } return results; } // Even shorter: function extractQuotedTextES6b(str) { return Array.from(REGEX_ES6.execAll(str), x => x[1]); }

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4