You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
constassert=require('assert');constmatchAll=require('string.prototype.matchall');conststr='aabc';constnonRegexStr='ab';constglobalRegex=/[ac]/g;constnonGlobalRegex=/[bc]/i;// non-regex arguments are coerced into a global regexassert.deepEqual([...matchAll(str,nonRegexStr)],[...matchAll(str,newRegExp(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 throwmatchAll.shim();// will be a no-op if not needed// non-regex arguments are coerced into a global regexassert.deepEqual([...str.matchAll(nonRegexStr)],[...str.matchAll(newRegExp(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
About
Spec-compliant polyfill for String.prototype.matchAll ESnext proposal.