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 🙏

© 2024 – Pkg Stats / Ryan Hefner

regex-to-strings

v2.1.0

Published

Expand a regular expression pattern into a set of words

Downloads

26,609

Readme

regex-to-strings

npm package node version npm type definitions Tests codecov Known Vulnerabilities

Generate strings that match a Regular Expression pattern. Efficiently generate all possible matches, or only the quantity you need.

Have you ever:

  • scrutinized a Regular Expression while trying to visualize what it does and doesn't match?
  • crafted a list of strings to test whether a Regular Expression is working as intended?
  • needed to generate filler data like phone numbers, addresses, zip codes, and email addresses?

regex-to-strings helps with problems like these!

Demo

API

expand(regExPattern)

The regExPattern parameter supports three formats:

  1. A RegExp object, like /[a-z]/i
  2. A string that looks like a RegExp object, like "/[a-z]/i"
  3. A string containing just a Regular Expression pattern, like "[a-z]"

The returned object contains two properties:

  • count: The total number of strings that match regExPattern
  • getIterator(): A generator that yields strings matched by regExPattern
import { expand } from 'regex-to-strings';

const phoneNumberPattern = /((\(555\) ?)|(555-))?\d{3}-\d{4}/;
const phoneNumberExpander = expand(phoneNumberPattern);

console.log(phoneNumberExpander.count); // 40000000

for (const phoneNumber of phoneNumberExpander.getIterator()) {
	console.log(phoneNumber);
	// (555)547-4836
	// 476-2063
	// 467-2475
	// (555) 194-2532
	// (555)403-4986
	// 555-838-9771
	// etc.
}

count(regExPattern)

A shortcut to the count property of expand(regExPattern).

import { count } from 'regex-to-strings';

const numStrings = count(/[a-z]{5}/i);
console.log(numStrings); // 380204032

expandN(regExPattern, n)

A shortcut to take n strings from expand(regExPattern).getIterator().

import { expandN } from 'regex-to-strings';

const strings = expandN(/\d{3,5}/, 5);
console.log(strings); // ['84504', '94481', '3971', '69398', '7792']

If the Regular Expression matches fewer than n strings, the returned array will contain fewer than n elements.

import { expandN } from 'regex-to-strings';

const strings = expandN(/[abc]/, 100);
console.log(strings); // ['b', 'a', 'c']

expandAll(regExPattern)

A shortcut to get all strings from expand(regExPattern).getIterator().

import { expandAll } from 'regex-to-strings';

const strings = expandAll(/\d/);
console.log(strings); // ['6', '5', '0', '2', '7', '9', '4', '3', '1', '8']

Notes

Supported Regular Expression syntax

regex-to-strings uses regexp-tree to parse your Regular Expression, and so the Regular Expression syntax you can use is largely determined by that library. If your pattern is not recognized by regex-to-strings, try parsing it with regexp-tree to see if the syntax is supported.

regex-to-strings also includes extensive positive and negative tests that track which Regular Expression features are supported.

Regular Expressions with unbounded repetition

Regular Expressions support many techniques for matching an unlimited number of characters. For example, the following patterns will match as many a's as possible:

  • /a*/
  • /a+/
  • /a{7,}/

When regex-to-strings encounters a repetition with no upper bound, it artificially sets an upper bound of 100. This is done for many reasons, chiefly so that a simple pattern like expandAll(/a+/) will not cause an infinite loop.

This also affects the count calculation; expand(/a+/).count returns 100 rather than Infinity.

Randomizing the results

regex-to-strings goes to great lengths to randomize the generated strings. Otherwise the results would be predictable, uninteresting, and probably unhelpful.

// How results might appear without randomization
const strings = expandN(/\d+/, 10);
console.log(strings); // ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

Random selections occur throughout the string generation process to give you a thorough sampling of matching strings.

Credits

regex-to-strings relies heavily on the regexp-tree Regular Expression parser by Dmitry Soshnikov. Thanks!