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-fun

v2.0.3

Published

Build regular expressions with functions

Downloads

1,685

Readme

Build regular expressions with functions.

Quick example

const anyGreeting = either('howdy', 'hi', 'hey')
const regex = combine(anyGreeting, optional(','), ' ', capture(/\w+/))
regex // => /(?:howdy|hi|hey)(?:,)? (\w+)/
'hey bub'.match(regex)[1] // => 'bub'

API

Functions return a regular expression without flags. If you want any flags, call the flags function last.

Regular expression input may be either a RegExp or a string. If it is a string, regex characters will be escaped - anyNumber('a+') will match any number of occurrences of a+ in a string (/a\+*/).

const {
	combine,
	flags,
	capture,
	either,

	anyNumber,
	oneOrMore,
	optional,
	exactly,
	atLeast,
	between,

	anyNumberNonGreedy,
	oneOrMoreNonGreedy,
	optionalNonGreedy,
	exactlyNonGreedy,
	atLeastNonGreedy,
	betweenNonGreedy,

} = require('regex-fun')

combine(...input)

combine(/sup/, 'd*g') // => /supd\*g/

either(...input)

either(/this/, /that/, 'other thing') // => /(?:this|that|other thing)/

capture(...input)

capture(/\w+/, either('this', 'that')) // => /(\w+(?:this|that))/

flags(flags, ...input)

flags('gm', /HOWDY/i) // => /HOWDY/gm

Greedy matching

anyNumber(...input)

anyNumber('wat') // => /(?:wat)*/

oneOrMore(...input)

oneOrMore('wat') // => /(?:wat)+/

optional(...input)

optional('wat') // => /(?:wat)?/

exactly(n, ...input)

exactly(2, 'wat') // => /(?:wat){2}/

atLeast(n, ...input)

atLeast(3, 'wat') // => /(?:wat){3,}/

between(n, m, ...input)

between(4, 5, 'wat') // => /(?:wat){4,5}/

Non-greedy matching

anyNumberNonGreedy(...input)

anyNumberNonGreedy('wat') // => /(?:wat)*?/

oneOrMoreNonGreedy(...input)

oneOrMoreNonGreedy('wat') // => /(?:wat)+?/

optionalNonGreedy(...input)

optionalNonGreedy('wat') // => /(?:wat)??/

exactlyNonGreedy(n, ...input)

exactlyNonGreedy(2, 'wat') // => /(?:wat){2}?/

atLeastNonGreedy(n, ...input)

atLeastNonGreedy(3, 'wat') // => /(?:wat){3,}?/

betweenNonGreedy(n, m, ...input)

betweenNonGreedy(4, 5, 'wat') // => /(?:wat){4,5}?/

Put it all together and you can do some cool stuff

This example is from verse-reference-regex, which finds and parses Bible verse ranges like "Revelation 13:5-6":

const requireVerse = true

const number = /(\d+)/
const numberAndOptionalLetter = /(\d+)([a-z])?/
const colonVerse = combine(':', numberAndOptionalLetter)
const chapterAndVerse = combine(number, requireVerse ? colonVerse : optional(colonVerse))

const secondHalfOfRange = combine(
	'-',
	either(
		/([a-z])/,
		/(\d+)([a-z])/,
		chapterAndVerse,
		numberAndOptionalLetter
	)
)
const range = combine(chapterAndVerse, optional(secondHalfOfRange))

const regexThatMatchesVerses = combine(
	capture(either(...bookNames, ...abbreviations)),
	' ',
	range
)

If you see a function missing, open a pull request, otherwise I'll add new functions as I need them.

License

WTFPL