Spec-compliant polyfill for String.prototype.matchAll
The npm package "string.prototype.matchall" is an essential tool for developers working with JavaScript, particularly those dealing with complex string manipulation tasks. This spec-compliant polyfill extends the String prototype with the `.matchAll()` method, following the ECMAScript specification closely. The primary purpose of this package is to enable developers to extract an iterator of all occurrences of a regular expression in a string. This can be incredibly beneficial when you need to handle multiple matches efficiently and perform actions on them, as the method returns all captured groups in an array format for each match, providing a detailed and structured way to handle string patterns.
To incorporate this functionality into your project, you can easily add it by running the command `npm install string.prototype.matchall`. This command integrates the polyfill into your node modules, allowing you to start using `.matchAll()` in environments where it might not be available by default, such as in older browsers or specific versions of Node.js. The installation process is straightforward and quick, thanks to npm's efficient package management, which means you can enhance your application's string processing capabilities without significant overhead or delay.
Moreover, the "string.prototype.matchall" package is well-maintained, with regular updates and a robust set of dependencies ensuring reliability and performance. The availability of this module via npm makes it accessible and easy to manage within your project's ecosystem. By using this polyfill, developers can ensure that their applications are compatible with the latest JavaScript standards, thus future-proofing their codebase and enhancing cross-environment compatibility. This makes the npm package an invaluable resource for developers looking to implement comprehensive and performant string handling functions in their JavaScript applications.
Core dependencies of this npm package and its dev dependencies.
call-bind, define-properties, es-abstract, es-errors, es-object-atoms, get-intrinsic, gopd, has-symbols, internal-slot, regexp.prototype.flags, set-function-name, side-channel, @es-shims/api, @ljharb/eslint-config, aud, auto-changelog, es5-shim, es6-shim, eslint, evalmd, for-each, functions-have-names, in-publish, mock-property, npmignore, nyc, object-inspect, object.assign, object.entries, safe-publish-latest, tape
A README file for the string.prototype.matchall code repository. View Code
ES2020 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 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
Simply clone the repo, npm install
, and run npm test