1 | # string.prototype.matchall <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
|
---|
2 |
|
---|
3 | [![github actions][actions-image]][actions-url]
|
---|
4 | [![coverage][codecov-image]][codecov-url]
|
---|
5 | [![dependency status][deps-svg]][deps-url]
|
---|
6 | [![dev dependency status][dev-deps-svg]][dev-deps-url]
|
---|
7 | [![License][license-image]][license-url]
|
---|
8 | [![Downloads][downloads-image]][downloads-url]
|
---|
9 |
|
---|
10 | [![npm badge][npm-badge-png]][package-url]
|
---|
11 |
|
---|
12 | ES2020 spec-compliant shim for String.prototype.matchAll. Invoke its "shim" method to shim `String.prototype.matchAll` if it is unavailable or noncompliant.
|
---|
13 |
|
---|
14 | This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES3-supported environment, and complies with the [spec](https://tc39.es/ecma262/#sec-string.prototype.matchall).
|
---|
15 |
|
---|
16 | Most common usage:
|
---|
17 | ```js
|
---|
18 | const assert = require('assert');
|
---|
19 | const matchAll = require('string.prototype.matchall');
|
---|
20 |
|
---|
21 | const str = 'aabc';
|
---|
22 | const nonRegexStr = 'ab';
|
---|
23 | const globalRegex = /[ac]/g;
|
---|
24 | const nonGlobalRegex = /[bc]/i;
|
---|
25 |
|
---|
26 | // non-regex arguments are coerced into a global regex
|
---|
27 | assert.deepEqual(
|
---|
28 | [...matchAll(str, nonRegexStr)],
|
---|
29 | [...matchAll(str, new RegExp(nonRegexStr, 'g'))]
|
---|
30 | );
|
---|
31 |
|
---|
32 | assert.deepEqual([...matchAll(str, globalRegex)], [
|
---|
33 | Object.assign(['a'], { index: 0, input: str, groups: undefined }),
|
---|
34 | Object.assign(['a'], { index: 1, input: str, groups: undefined }),
|
---|
35 | Object.assign(['c'], { index: 3, input: str, groups: undefined }),
|
---|
36 | ]);
|
---|
37 |
|
---|
38 | assert.throws(() => matchAll(str, nonGlobalRegex)); // non-global regexes throw
|
---|
39 |
|
---|
40 | matchAll.shim(); // will be a no-op if not needed
|
---|
41 |
|
---|
42 | // non-regex arguments are coerced into a global regex
|
---|
43 | assert.deepEqual(
|
---|
44 | [...str.matchAll(nonRegexStr)],
|
---|
45 | [...str.matchAll(new RegExp(nonRegexStr, 'g'))]
|
---|
46 | );
|
---|
47 |
|
---|
48 | assert.deepEqual([...str.matchAll(globalRegex)], [
|
---|
49 | Object.assign(['a'], { index: 0, input: str, groups: undefined }),
|
---|
50 | Object.assign(['a'], { index: 1, input: str, groups: undefined }),
|
---|
51 | Object.assign(['c'], { index: 3, input: str, groups: undefined }),
|
---|
52 | ]);
|
---|
53 |
|
---|
54 | assert.throws(() => matchAll(str, nonGlobalRegex)); // non-global regexes throw
|
---|
55 |
|
---|
56 | ```
|
---|
57 |
|
---|
58 | ## Tests
|
---|
59 | Simply clone the repo, `npm install`, and run `npm test`
|
---|
60 |
|
---|
61 | [package-url]: https://npmjs.com/package/string.prototype.matchall
|
---|
62 | [npm-version-svg]: https://versionbadg.es/es-shims/String.prototype.matchAll.svg
|
---|
63 | [deps-svg]: https://david-dm.org/es-shims/String.prototype.matchAll.svg
|
---|
64 | [deps-url]: https://david-dm.org/es-shims/String.prototype.matchAll
|
---|
65 | [dev-deps-svg]: https://david-dm.org/es-shims/String.prototype.matchAll/dev-status.svg
|
---|
66 | [dev-deps-url]: https://david-dm.org/es-shims/String.prototype.matchAll#info=devDependencies
|
---|
67 | [npm-badge-png]: https://nodei.co/npm/string.prototype.matchall.png?downloads=true&stars=true
|
---|
68 | [license-image]: https://img.shields.io/npm/l/string.prototype.matchall.svg
|
---|
69 | [license-url]: LICENSE
|
---|
70 | [downloads-image]: https://img.shields.io/npm/dm/string.prototype.matchall.svg
|
---|
71 | [downloads-url]: https://npm-stat.com/charts.html?package=string.prototype.matchall
|
---|
72 | [codecov-image]: https://codecov.io/gh/es-shims/String.prototype.matchAll/branch/main/graphs/badge.svg
|
---|
73 | [codecov-url]: https://app.codecov.io/gh/es-shims/String.prototype.matchAll/
|
---|
74 | [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/String.prototype.matchAll
|
---|
75 | [actions-url]: https://github.com/es-shims/String.prototype.matchAll/actions
|
---|