npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@byojs/matchall-starts-at

v0.0.0-pre-202605291121

Published

Hopeful/future polyfill: add start-index behavior to matchAll() regex/string matching

Readme

@byojs/matchall-starts-at

A BYOJS prollyfill adding explicit start-position support to JavaScript's regex match iteration APIs.

JavaScript's native String.prototype.matchAll(..) already clones a supplied RegExp internally, but it also copies the regex's current lastIndex onto that clone. That preserves legacy regex cursor behavior, but it means matchAll(..) is still vulnerable to unexpected regex state.

This package preserves the existing default behavior, while adding an explicit startsAt parameter; to get a fresh, starting-from-the-beginning behavior, always pass 0 as the second argument:

import "@byojs/matchall-starts-at";

var re = /a/g;
re.lastIndex = 2;

[..."baaa".matchAll(re)].map(m => m.index);    // [ 2, 3 ]

[..."baaa".matchAll(re,0)].map(m => m.index);  // [ 1, 2, 3 ]

[..."baaa".matchAll(re,3)].map(m => m.index);  // [ 3 ]

This is not a polyfill, in that there's currently no standards track proposal to add this to JS (BUT THERE SHOULD BE!).

It's a prollyfill: a speculative API improvement -- suggestion, future proposed polyfill -- for codebases that want a more explicit match-iteration cursor.

Why?

Native matchAll(..) effectively behaves like this:

var clone = new RegExp(re);
clone.lastIndex = re.lastIndex;

This package gives you an explicit way to choose the starting position without mutating the original regex:

var clone = new RegExp(re);
clone.lastIndex = startsAt;

So this:

str.matchAll(re,0)

is the convenient equivalent of:

var clone = new RegExp(re);
clone.lastIndex = 0;

str.matchAll(clone);

Usage

Import the package (only needed once since it patches global prototypes):

import "@byojs/matchall-starts-at";

After import, both APIs accept an optional finite-number startsAt argument:

str.matchAll(re,startsAt);
re[Symbol.matchAll](str,startsAt);

The existing one-argument behavior is preserved; existing one-argument calls keep their native-compatible behavior.

var re = /a/g;
re.lastIndex = 2;

[..."baaa".matchAll(re)].map(m => m.index);
// [ 2, 3 ]

re.lastIndex;
// 2

To ignore a polluted lastIndex and start from the beginning, pass 0 for the second argument:

var re = /a/g;
re.lastIndex = 2;

[..."baaa".matchAll(re,0)].map(m => m.index);
// [ 1, 2, 3 ]

re.lastIndex;
// 2

To start at another explicit offset:

[..."a-a-a-a".matchAll(/a/g,4)].map(m => m.index);
// [ 4, 6 ]

Behavior

This package patches:

RegExp.prototype[Symbol.matchAll]

String.prototype.matchAll

It only activates the new behavior when startsAt is a finite number:

"baaa".matchAll(/a/g,0);         // explicit startsAt
"baaa".matchAll(/a/g,2);         // explicit startsAt

"baaa".matchAll(/a/g);           // native-compatible behavior
"baaa".matchAll(/a/g,undefined); // native-compatible behavior
"baaa".matchAll(/a/g,NaN);       // native-compatible behavior
"baaa".matchAll(/a/g,Infinity);  // native-compatible behavior
"baaa".matchAll(/a/g,"0");      // native-compatible behavior

This narrower behavior is intentional. It avoids claiming arbitrary future second-argument shapes that JavaScript may eventually define.

That narrow activation is the main reason this package can justify patching global prototypes despite the usual "don't touch globals" rule.

Error on non /g regular expressions

Non-global regex behavior for String.prototype.matchAll(..) is preserved:

"abc".matchAll(/a/,0); // TypeError, same as native matchAll(..)

The direct RegExp.prototype[Symbol.matchAll](..) path follows the platform's lower-level behavior.

Caution

This package intentionally modifies built-in prototypes.

That is appropriate for application code and controlled runtimes that deliberately opt into this behavior. It should not be imported by libraries that run inside other people's applications.

TypeScript Support

Type definitions for the patched/prolyfilled native prototype methods (RegExp.prototype[Symbol.matchAll] and String.prototype.matchAll()) are bundled with the package (in an external .d.ts file).

TypeScript projects will pick the definitions up automatically; no separate @types/ install needed.

Tests

A test suite is included in this repository, as well as the npm package distribution. The default test behavior runs the test suite using the files in src/.

To run the test suite:

npm test

License

License

All code and documentation are (c) 2026 Kyle Simpson and released under the MIT License. A copy of the MIT License is also included.