string-match-all

`String.prototype.matchAll` ponyfill.

Usage no npm install needed!

<script type="module">
  import stringMatchAll from 'https://cdn.skypack.dev/string-match-all';
</script>

README

string-match-all

Build Status Browser testing by BrowserStack

String.prototype.matchAll ponyfill.

The String.prototype.matchAll returns an iterator of all results matching a string against a regular expression, including capturing groups.

Install

npm install string-match-all --save

Usage

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!

API

matchAll(string, matcher)

Returns: Iterator

string

Type: string

String to match.

matcher

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.

Browser support

Tested in Chrome 91, Firefox 90, Internet Explorer 11 and should work in all modern browsers (support based on Browserslist configuration).

Test

Test suite is taken and modified from es-shims test suite.

For automated tests, run npm run test:automated (append :watch for watcher support).

License

MIT © Ivan Nikolić