String.prototype.matchAll
ponyfill.
The
String.prototype.matchAll
returns an iterator of all results matching a string against a regular expression, including capturing groups.
npm install string-match-all --save
import matchAll from 'string-match-all';
const matches = [...matchAll('test1test2', /t(e)(st(\d?))/g)];
// ["test1", "e", "st1", "1", index: 0, input: "test1test2"]
// ["test2", "e", "st2", "2", index: 5, input: "test1test2"]
You can use named export preferNative
if you wish to use native
implementation if it’s available. In all other cases, ponyfill will be used.
Beware of
caveats!
Returns: Iterator
Type: string
String to match.
Type: string|RegExp
Value to match original string.
If a non-RegExp
object is passed, it is implicitly converted to a RegExp
by
using new RegExp(regexp, 'g')
.
The RegExp
object must have the global
flag, otherwise a TypeError
will be
thrown.
Tested in Chrome 91, Firefox 90, Internet Explorer 11 and should work in all modern browsers (support based on Browserslist configuration).
Test suite is taken and modified from es-shims test suite.
For automated tests, run npm run test:automated
(append :watch
for watcher
support).
MIT © Ivan Nikolić