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

jrex

v0.2.4

Published

Modern Javascript library for composable, iterable, lazy, and immutable regular expressions

Readme

jRex

A modern Javascript library for composable, iterable, lazy, and immutable regular expressions.

Example 1: lazy iteration and method chaining:

 jRex(/\w+/)
        .filter( function(r) { return r.text().length < 6; } )
        .last()
        .map( function(r) { return 'last small word: ' + r; } )
        .eval( 'search the last word smaller than six characters' )

evaluates to:

'last small word: six'

Example 2: Composing regex:

jRex({or:['abc','def'],flags:'i'}).regex()

evaluates to:

/(?:abc)|(?:def)/i

Example 3: text replace:

jRex(/([xyz])/)
    .filter( function(r) { return r.index() % 2 == 0; })
    .format('<$1>').replace('playing my xylophone')

evaluates to:

pla<y>ing m<y> <x>ylophone

Installation

npm install jrex

Usage

var jRex = require('jrex');
jRex(...)...;

API definition

Patterns

  • jRex(element, flags?).chainFunction1(...).chainFunctionN(...).endFunction(...)
  • jRex(...).getterFunction()

Element

Any element, including the root element being the first argument of jRex, is either a:

  • hash map of the form: {<primary field>: ..., <secondary field1>: ..., ...., <secondary fieldN>: ...}
  • a string being treated as text. Therefore it is escaped within the regular expression.
  • an array of sub elements. The sub elements must sequentially match the input string.

Primary fields are:

  • or: An array of elements where any of the elements matches the input string.
  • text: Any element is treated/converted to text, and handled as defined previously.
  • regex: Any element being either a regular expression, treated as such but stripped of its flags, or converted to text and parsed as a regular expression, without the slashes and the flags.
  • sub: Any sub element.
  • any: Any character.
  • in: Match any from a collection or ranges of characters, as defined by RegExp's [...] notation, where the brackets are omitted.
  • out: Match everything except any from a collection or ranges of charactes, as defined by RegExp's [^...], where the brackets and '^' are omitted.

Secondary fieds are:

  • store: values: true. This will store the element, like /(...)/.
  • close: values: start|end|both. This will close the element, like /^...$/.
  • ahead: values: true|'not'. This will wrap the element like /(?=...)/ or not: /(?!...)/.
  • min: values: any number. This will enable repeat and define the minimal amount of repeats.
  • max: values: any number. This will enable repeat and define the maximum amount of repeats.
  • exact: values: any number. Equivalent to {...min: value, max: value...}
  • lazy: values: true. This will result in lazy parsing, like /.??/ or /.*?/.

Chain functions

These chain/cascading methods transform and filter the results:

map( func: (result)=>any ): 
 	It transforms the result as specified by the function.
captures(): 
	Equivalent to map((r)=>r.captures()).
text(key?: number): 
	Equivalent to map((r)=>r.text(key)).
index(): 
	Equivalent to map((r)=>r.index()).
filter(func: (result)=>boolean): 
	It filters outs all results for which the function evaluates to a falsy value.
while(func: (result)=>boolean): 
	It includes results until the fuction evaluates to a falsy value.
henceforth(func: (result)=>boolean): 
	It excludes results until the fuction evaluates to a truthy value.
reduce(func: (accu, value)=>accu, accuValue/accuFactoryFunc?):
	Reduces by reiterating accu=func(accu,value). If initial value or factory function is not supplied,
	it will use the first value as accumulator.
collect(number):
	It takes the first 'number' results and stops.
skip(number): 
	It excludes the first 'number' results.
first(), last(): 
	It will return either the first or last result.
format(fmt: string): 
	A string with special meaning for $&, $1...$nn, or $$. (See javascript replace function).

When using these functions, it will always use the regular expression in 'global' mode.

Result

As long as no mapping functions is applied, the supplied result argument is an object with the following functions:

after(): string
	Returns the complete text after the match.
between(): string
	Returns the texts between this match and the previous match or beginning.
before(): string
	Returns the complete text before the match.
endIndex(): number
	Returns the character position after the match.
index(): number
	Return the character position of the start of the match.
input(): string
	Returns the complete text.
text(key?: number): string
	Returns the captured text. If the number is 0 or falsy, it returns the whole matched text.
captures(): number[] {
	Returns the information about the captures, excluding the information about the whole matched text.
setNextSearch(pos: number): void
	Sets the character position of the next search.

End functions

These functions will initiate the execution:

eval(text, startPos?): any
	evaluates the result.
test(text, startPos?): boolean 
	returns whether there is at least one result.
replace(text, startPos?): string
	replaces the matches with strings.
split(text, startPos?): string[]
	splits the function on matches that have not been filtered out.

Getter functions

These functions return information about the regular expression:

flags(): string
	returns the flags in one string.
regex(): RegExp 
	returns the regular expression.
toJSON(): any
	returns a json representation that can be fed back to jRex.