Skip to content
Spec-compliant polyfill for String.prototype.matchAll ESnext proposal.
JavaScript
Branch: master
Clone or download
Pull request Compare This branch is even with es-shims:master.
Latest commit 565fae0 Dec 23, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github [actions] add automatic rebasing / merge commit blocking Dec 13, 2019
helpers [Refactor] use `internal-slot` Dec 13, 2019
test [Breaking] throw on non-global/nullish flags Oct 3, 2019
.eslintignore [Dev Deps] update `eslint`, `@ljharb/eslint-config` Sep 28, 2017
.eslintrc
.gitignore Only apps should have lockfiles. Jul 31, 2017
.npmrc [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `evalmd`, `funct… Aug 31, 2019
.travis.yml [Tests] use shared travis-ci configs Dec 13, 2019
CHANGELOG.md v4.0.2 Dec 23, 2019
LICENSE Initial commit Jul 28, 2015
README.md [Breaking] throw on non-global/nullish flags Oct 3, 2019
auto.js [New] add `auto` entry point Jan 9, 2018
implementation.js [Refactor] use split-up `es-abstract` (61% bundle size decrease) Dec 13, 2019
index.js [Deps] update `es-abstract` Nov 14, 2019
package.json v4.0.2 Dec 23, 2019
polyfill-regexp-matchall.js [Refactor] provide a consistent way to determine the polyfill for `Re… Apr 25, 2019
polyfill.js [Breaking] throw on non-global/nullish flags Oct 3, 2019
regexp-matchall.js [Refactor] use split-up `es-abstract` (61% bundle size decrease) Dec 13, 2019
shim.js [Refactor] provide a consistent way to determine the polyfill for `Re… Apr 25, 2019

README.md

string.prototype.matchall Version Badge

Build Status dependency status dev dependency status License Downloads

npm badge

browser support

ES Proposal spec-compliant shim for String.prototype.matchAll. Invoke its "shim" method to shim String.prototype.matchAll if it is unavailable or noncompliant.

This package implements the es-shim API interface. It works in an ES3-supported environment, and complies with the proposed spec.

Most common usage:

const assert = require('assert');
const matchAll = require('string.prototype.matchall');

const str = 'aabc';
const nonRegexStr = 'ab';
const globalRegex = /[ac]/g;
const nonGlobalRegex = /[bc]/i;

// non-regex arguments are coerced into a global regex
assert.deepEqual(
	[...matchAll(str, nonRegexStr)],
	[...matchAll(str, new RegExp(nonRegexStr, 'g'))]
);

assert.deepEqual([...matchAll(str, globalRegex)], [
	Object.assign(['a'], { index: 0, input: str, groups: undefined }),
	Object.assign(['a'], { index: 1, input: str, groups: undefined }),
	Object.assign(['c'], { index: 3, input: str, groups: undefined }),
]);

assert.throws(() => matchAll(str, nonGlobalRegex)); // non-global regexes throw

matchAll.shim(); // will be a no-op if not needed

// non-regex arguments are coerced into a global regex
assert.deepEqual(
	[...str.matchAll(nonRegexStr)],
	[...str.matchAll(new RegExp(nonRegexStr, 'g'))]
);

assert.deepEqual([...str.matchAll(globalRegex)], [
	Object.assign(['a'], { index: 0, input: str, groups: undefined }),
	Object.assign(['a'], { index: 1, input: str, groups: undefined }),
	Object.assign(['c'], { index: 3, input: str, groups: undefined }),
]);

assert.throws(() => matchAll(str, nonGlobalRegex)); // non-global regexes throw

Tests

Simply clone the repo, npm install, and run npm test

You can’t perform that action at this time.