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

mol_regexp

v0.0.1156

Published

Builds composable regular expression, which compiles to native RegExp. And generates a matching string for it.

Downloads

2,715

Readme

$mol_regexp

Builds composable regular expression, which compiles to native RegExp. And generates a matching string for it.

Instance API

All Matched Substrings

// [ 'foo', 'bar' ]
'foo bar'.match( word )

Tokenize String

// [ 'foo', 'bar' ]
for( const token of 'foo bar'.matchAll( word ) ) {
	
	// Full matched substring
	token[0]
	
	// Catched subgroups by names like `{ foo: 'bar', ... }`
	// `undefined` when token doesn't matched.
	token.groups
	
}

Generate New String

Substitution

// "[email protected]"
mail.generate({
	dot_atom: 'foo',
	domain: 'example.org',
})

Validation

// Error: "Wrong param: dot_atom=jin."
mail.generate({
	dot_atom: 'jin.',
	domain: 'example.org',
})

Default Value from Pattern

// "jin: male"
sexism.generate({
	name: 'jin',
	male: true,
})

Build Instance

More examples in tests.

Fixed String

// /:\)/gsu
const smile = $mol_regexp.from( ':)' )

| Matches |-------- | :)

Flags

// /hello/gimsu
const hello = $mol_regexp.from( 'hello', {
	ignoreCase: true,
	multiline: true,
} )

| Matches |-------- | hello | HELLO

From Other Regexp

// /.../gmsu
const triplet = $mol_regexp.from(
	$mol_regexp.from(
		/.../,
		{ ignoreCase: true },
	),
	{ multiline: true },
)

| Matches |-------- | any | a=+

Char by code point

// /\u{20}/gsu
const space = $mol_regexp.from( 32 )

Inclusive char class

// /[,;\u{20}\u{61}-\u{7a}\d]/gsu
const tags = $mol_regexp.char_only(
	',;', // chars
	32, // code of space
	$mol_regexp.char_range( 0x61, 0x7A ), // a-z
	$mol_regexp.decimal_only, // 0-9
)

| Matches |-------- | foo, bar | 0;1;10

Exclusive char class

// /[^,;\u{20}\u{61}-\u{7a}\d]/gsu
const tags = $mol_regexp.char_except(
	',;', // chars
	32, // code of space
	$mol_regexp.char_range( 0x61, 0x7A ), // a-z
	$mol_regexp.decimal_only, // 0-9
)

| Matches |-------- | *=+ | ABC

Unicode classes

// /\p{Script=Cyrillic}/gsu
$mol_regexp.unicode_only( 'Script', 'Cyrillic' )

// /\P{Script=Cyrillic}/gsu
$mol_regexp.unicode_except( 'Script', 'Cyrillic' )

// /\p{Hex_Digit}/gsu
$mol_regexp.unicode_only( 'Hex_Digit' )

// /\P{Hex_Digit}/gsu
$mol_regexp.unicode_except( 'Hex_Digit' )

Repeat non-greedy

// Regexp: /(?:\n){1,2}?/gsu
const para_sep = $mol_regexp.repeat( '\n', 1, 2 )

| Matches |-------- | \n | \n\n

Repeat greedy

// /(?:\d){2,4}/
const year = $mol_regexp.repeat_greedy( $mol_regexp.decimal_only, 2, 4 )

| Matches |-------- | 95 | 2020

Sequence and optional sequence

// /(?:\d){2,4}(?:\.\.(?:\d){2,4}){0,1}/gsu
const life_years = $mol_regexp.from([ year, [ '..', year ] ])

| Matches |-------- | 95..99 | 2020

Catch groups

const from = year
const to = year

// /(?:((?:\d){2,4}))(?:\.\.(?:((?:\d){2,4}))){0,1}/gsu
const life_years = $mol_regexp.from([ {from}, [ '..', {to} ] ])

| Matches | from | to |---------|------|---- | 95..99 | 95 | 99 | 2020 | 2020 |

Сatch variants

enum Sex {
	male = 'male',
	female = 'female',
}

// /(?:((?:(male)|(female))))/gsu
const sex = $mol_regexp.from( {Sex} )

// { Sex: string, male: string, female: string } | undefined
const res = [ ... text.matchAll( sex ) ][0].groups

| Matches | Sex | male | female |---------|--------|-----------|------- | male | male | male | | female | female | | female

Complex example

const {
	begin, end,
	char_only, char_range,
	latin_only, slash_back,
	repeat_greedy, from,
} = $mol_regexp

const atom_char = char_only( latin_only, "!#$%&'*+/=?^`{|}~-" )
const atom = repeat_greedy( atom_char, 1 )
const dot_atom = from([ atom, repeat_greedy([ '.', atom ]) ])

const name_letter = char_only(
	char_range( 0x01, 0x08 ),
	0x0b, 0x0c,
	char_range( 0x0e, 0x1f ),
	0x21,
	char_range( 0x23, 0x5b ),
	char_range( 0x5d, 0x7f ),
)

const quoted_pair = from([
	slash_back,
	char_only(
		char_range( 0x01, 0x09 ),
		0x0b, 0x0c,
		char_range( 0x0e, 0x7f ),
	)
])

const name = repeat_greedy({ name_letter, quoted_pair })
const quoted_name = from([ '"', {name}, '"' ])

const local_part = from({ dot_atom, quoted_name })
const domain = dot_atom

const mail = from([ begin, local_part, '@', {domain}, end ])

| Matches | domain | dot_atom | name | name_letter | quoted_name | quoted_pair |-----------|--------|----------|------|-------------|-------------|------------ | [email protected] | c.d | a.b | | | | | "a\n"@c.d | c.d | | a\n | | "a\n" | \n

Usage from NPM

npm install mol_regexp

import { $mol_regexp } from 'mol_regexp'

Sandbox