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

regexp-manager

v0.19.1

Published

regexp builder for node.js developer

Downloads

45

Readme

regexp-manager

regexp-manager

$ npm i regexp-manager

This library creates regular expressions in a form similar to query builders. The benefits of this library are three.

  1. To intuitively use various methods of regular expressions that developers do not usually deal with
  2. To increase the reuse of regular expressions
  3. To infer regular expressions at the type level and allow problems to be found at the time of compilation

How to use a builder

import { RegExpPatternBuilder } from 'regexp-manager';

/**
 * result : '(010|011)[0-9]{3,4}[0-9]{4,4}'
 */
const koreanPhoneNumber = new RegExpPatternBuilder()
    .capturing((qb) => qb.and('010').or('011')) // '(010|011)'
    .and('-') // '-'
    .and((qb) => qb, and('[0-9]').between(3, 4)) // '[0-9]{3,4}'
    .and('-') // '-'
    .and((qb) => qb.and('[0-9]').between(4, 4)).expression; // '[0-9]{4,4}'

If you write a function that returns the string or additional builder according to the inferred type, you can check the result at the time of compilation. If you want to use a sub-builder inside, you can use the qb given as a parameter. It works even if you just write it as a string.

Methods currently implemented

or

It means 'or syntax' using pipe characters in regular expressions. The 'or syntax' can also be checked in advance by type level.

// It's also inferred from the type.

const leftOrRight = new RegExpPatternBuilder('left').or('right').expression; // 'left|right'
const redOrBlue = new RegExpPatternBuilder('red').or('blue').expression; // 'red|blue'
const dogOrCat = new RegExpPatternBuilder('dog').or('cat').expression; // 'dog|cat'

and

The 'and syntax' simply connects strings. In a regular expression, sometimes it is more readable to divide and combine characters in semantic units.

// It's also inferred from the type.

const leftOrRight = new RegExpPatternBuilder('left').and('right').expression; // 'leftright'
const redOrBlue = new RegExpPatternBuilder('red').and('blue').expression; // 'redblue'
const dogOrCat = new RegExpPatternBuilder('dog').and('cat').expression; // 'dogcat'

capturing

There is a syntax called 'capturing' in the regular expression. This syntax uses parentheses to lock the string, making the test, match method of the regular expression different in the future.

// It's also inferred from the type.
const capturingA = new RegExpPatternBuilder().capturing('A').expression; // '(A)'

quantifier methods

There are four methods in quantifier: lessThan, lessThanOrEqual, moreThan, and moreThanOrEqual. They define how many times a previously specified character or string will exist based on a given number. if you want, you can use between. it will be easy to use builder.

// `lessThan` method
// Below return 'a{1,9}'. And it's also inferred from the type.
const lessThanTen = new RegExpPatternBuilder('a').lessThan(10).expression;

// `lessThanOrEqual` method
// Below return 'a{1,10}'. And it's also inferred from the type.
const lessThanOrEqualTen = new RegExpPatternBuilder('a').lessThanOrEqual(10).expression;

// `moreThan` method
// Below return 'a{4,}'. And it's also inferred from the type.
const moreThanThree = new RegExpPatternBuilder('a').moreThan(3).expression;

// `moreThanOrEqual` method
// Below return 'a{3,}'. And it's also inferred from the type.
const moreThanOrEqualThree = new RegExpPatternBuilder('a').moreThanOrEqual(3).expression;

includes

It means lookahead, lookbehind of the regular expression. These are strings that should be included in a string but should not be caught in a regular expression.

// `include`("left", P) method means lookbehind
// Below return '(?<=a)b'. And it's also inferred from the type.
const lookhehind = new RegExpPatternBuilder('b').includes('LEFT', 'a').expression;

// `include`("right", P) method means lookahead
// Below return 'b(?=a)'. And it's also inferred from the type.
const lookahead = new RegExpPatternBuilder('b').includes('RIGHT', 'a').expression;

excludes

It means lookahead, lookbehind of the regular expression. These are strings that should be excluded in a string.

// `exclude`("left", P) method means negative lookbehind
// Below return '(?<!a)b'. And it's also inferred from the type.
const nagativeLookbhind = new RegExpPatternBuilder('b').excludes('LEFT', 'a').expression;

// `exclude`("right", P) method means negative lookbehind
// Below return 'b(?!a)'. And it's also inferred from the type.
const negativeLookahead = new RegExpPatternBuilder('b').excludes('RIGHT', 'a').expression;

beginning

const builder = new RegExpPatternBuilder('abc').beginning().expression; // '^abc'

range

const expression = new RegExpPatternBuilder().range('1-10').expression; // '1-10'
const expression = new RegExpPatternBuilder().range(1, 10).expression; // '1-10'

optional

const expression = new RegExpPatternBuilder().optional('abc').expression; // 'abc?'