npm install string.prototype.matchall

Spec-compliant polyfill for String.prototype.matchAll

About 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.

More from es-shims

es-shims npm packages

Find the best node modules for your project.

Search npm

regexp.prototype.flags

ES6 spec-compliant RegExp.prototype.flags shim...

Read more
,

string.prototype.trimend

ES2019 spec-compliant String.prototype.trimEnd shim...

Read more
,

string.prototype.trimstart

ES2019 spec-compliant String.prototype.trimStart shim...

Read more
,

arraybuffer.prototype.slice

ES spec-compliant shim for ArrayBuffer.prototype...

Read more
,

string.prototype.matchall

Spec-compliant polyfill for String.prototype...

Read more
,

object.groupby

An ESnext spec-compliant `Object.groupBy` shim/polyfill/replacement that works as far down as ES3...

Read more
,

array.prototype.tosorted

An ESnext spec-compliant `Array.prototype...

Read more
,

es-iterator-helpers

An ESnext spec-compliant iterator helpers shim/polyfill/replacement that works as far down as ES3...

Read more
,

array.prototype.toreversed

An ESnext spec-compliant `Array.prototype...

Read more
,

typedarray.prototype.slice

ES spec-compliant shim for TypedArray.prototype...

Read more
,

string.prototype.replaceall

Spec-compliant polyfill for String.prototype...

Read more
,

error-cause

An ES-spec-compliant shim/polyfill/replacement for the `...

Read more

Dependencies

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

Documentation

A README file for the string.prototype.matchall code repository. View Code

string.prototype.matchall Version Badge

github actions coverage dependency status dev dependency status License Downloads

npm badge

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

Tests

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