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

readable-regex

v1.0.0

Published

A library for dynamically generating regular expressions in a readable fashion

Readme

readable-regex

A Node.js library for dynamically generating regular expressions in a readable fashion

npm Version Build Status

Concept

Regular expressions, while incredibly powerful, are nasty to look at. The use all sorts of special characters and they are rarely constructed in code from smaller, named components. The idea of this library is to make it easier to read the components that go into a regular expression and to dynamically generate regular expressions using functions rather than string concatenation.

Example

const reg = require(__dirname + '/../index.js')
const SOME_DIGITS = reg.some(reg.DIGIT)
const DATE_CAPTURE = reg([
	reg.START,
	reg.capture(
		reg([
			reg.or(
				reg.capture(
					reg.some(
						reg.charIn(['a', 'z'], ['A', 'Z'])
					),
					'month-text'
				),
				reg.capture(
					SOME_DIGITS,
					'month-num'
				)
			),
			'-',
			reg.capture(
				SOME_DIGITS,
				'day'
			),
			'-',
			reg.capture(
				reg.times(reg.DIGIT, false, 2),
				'year'
			)
		]),
		'date'
	),
	reg.END
])
const match = reg.exec(DATE_CAPTURE, 'Jan-5-2017')
console.log(match.get('month-text')) //'Jan'

RegExp construction methods (more are planned)

It is not advisable to pass in regular expressions not generated by this library because they are all assumed to be concatenatable due to being previously wrapped in non-capturing groups.

  • reg(Array<string|RegExp>|string|RegExp[, Array<Flag>|Flag flags]): constructs a new regular expression by concatenating the component(s). If there is only one component, it doesn't need to be wrapped in an Array. Strings are interpreted as literal text and will be properly escaped (e.g. '\n[abc]' will become /(?:\n\[abc\])/). Flags are values of the object reg.f (global, case-insensitive, etc.).
  • reg.times(RegExp, boolean notGreedy, number min[, number max]): matches the regular expression at least min times and at most max times, if a max is passed in. Making it not greedy will cause it to try to capture as few occurences as possible.
  • reg.any(RegExp[, boolean notGreedy]): matches 0 or more occurences of the regular expression
  • reg.some(RegExp[, boolean notGreedy]): matches 1 or more occurences of the regular expression
  • reg.maybe(RegExp[, boolean notGreedy]): matches 0 or 1 occurences of the regular expression
  • reg.thisMany(RegExp, number times): matches times occurences of the regular expression
  • reg.capture(RegExp, string name): wraps the regular expression in a capturing group that can be referred to by name
  • reg.charIn(char|[char, char] ...chars): matches any of the specified characters. Individual characters specify a single character, whereas Arrays of 2 of them specify a range of characters.
  • reg.charNotIn(char|[char, char] ...chars): matches any character besides the specified characters. Individual characters specify a single character, whereas Arrays of 2 of them specify a range of characters.
  • reg.or(RegExp ...res): matches any instance of exactly one of the specified regular expressions

Functions

  • reg.exec(RegExp, string): like RegExp.prototype.match(string) except the returned match object, if not null, has a get function which will take in the name of a capture group and return what was captured (see the example above)

Constants

  • reg.ANY: .
  • reg.DIGIT: \d
  • reg.START: ^
  • reg.END: $
  • reg.ALPHANUM: \w
  • reg.NOT_ALPHANUM: \W
  • reg.WHITESPACE: \s
  • reg.NOT_WHITESPACE: \S
  • reg.WORD_BOUND: \b
  • reg.IN_WORD: \B

Flags

  • reg.f.GLOBAL: the g flag
  • reg.f.IGNORE_CASE: the i flag
  • reg.f.MULTILINE: the m flag
  • reg.f.UNICODE: the u flag
  • reg.f.STICKY: the y flag