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

ts-regex-builder

v1.8.2

Published

Maintainable regular expressions for TypeScript and JavaScript.

Downloads

97,556

Readme

npm version Build npm bundle size PRs Welcome Star on GitHub

TS Regex Builder

Build maintainable regular expressions for TypeScript and JavaScript.

API docs | Examples

Goal

Regular expressions are a powerful tool for matching text patterns, yet they are notorious for their hard-to-parse syntax, especially in the case of more complex patterns.

This library allows users to create regular expressions in a structured way, making them easy to write and review. It provides a domain-specific langauge for defining regular expressions, which are finally turned into JavaScript-native RegExp objects for fast execution.

// Regular JS RegExp
const hexColor = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;

// TS Regex Builder DSL
const hexDigit = /[a-fA-F0-9]/; // or: charClass(charRange('a', 'f'), charRange('A', 'F'), charRange('0', '9'));

const hexColor = buildRegExp([
  startOfString,
  optional('#'),
  capture(
    choiceOf(
      repeat(hexDigit, 6), // #rrggbb
      repeat(hexDigit, 3), // #rgb
    ),
  ),
  endOfString,
]);

Installation

npm install ts-regex-builder

or

yarn add ts-regex-builder

Basic usage

import { buildRegExp, capture, oneOrMore } from 'ts-regex-builder';

// /Hello (\w+)/
const regex = buildRegExp(['Hello ', capture(oneOrMore(word))]);

Regex domain-specific language

TS Regex Builder allows you to build complex regular expressions using domain-specific language.

Terminology:

  • regex construct (RegexConstruct) - common name for all regex constructs like character classes, quantifiers, and anchors.
  • regex element (RegexElement) - a fundamental building block of a regular expression, defined as either a regex construct, a string, or RegExp literal (/.../).
  • regex sequence (RegexSequence) - a sequence of regex elements forming a regular expression. For developer convenience, it also accepts a single element instead of an array.

Most of the regex constructs accept a regex sequence as their argument.

Examples of sequences:

  • single element (construct): capture('Hello')
  • single element (string): 'Hello'
  • single element (RegExp literal): /Hello/
  • array of elements: ['USD', oneOrMore(digit), /Hello/]

Regex constructs can be composed into a tree structure:

const currencyCode = repeat(charRange('A', 'Z'), 3);
const currencyAmount = buildRegExp([
  choiceOf('$', '€', currencyCode), // currency
  capture(
    oneOrMore(digit), // integer part
    optional(['.', repeat(digit, 2)]), // fractional part
  ),
]);

See Types API doc for more info.

Regex Builders

| Builder | Regex Syntax | Description | | ---------------------------------------- | ------------ | ----------------------------------- | | buildRegExp(...) | /.../ | Create RegExp instance | | buildRegExp(..., { ignoreCase: true }) | /.../i | Create RegExp instance with flags |

See Builder API doc for more info.

Regex Constructs

| Construct | Regex Syntax | Notes | | ------------------- | ------------ | ------------------------------- | | choiceOf(x, y, z) | x\|y\|z | Match one of provided sequences | | capture(...) | (...) | Create a capture group |

See Constructs API doc for more info.

[!NOTE] TS Regex Builder does not have a construct for non-capturing groups. Such groups are implicitly added when required.

Quantifiers

| Quantifier | Regex Syntax | Description | | -------------------------------- | ------------ | ------------------------------------------------- | | zeroOrMore(x) | x* | Zero or more occurrence of a pattern | | oneOrMore(x) | x+ | One or more occurrence of a pattern | | optional(x) | x? | Zero or one occurrence of a pattern | | repeat(x, n) | x{n} | Pattern repeats exact number of times | | repeat(x, { min: n, }) | x{n,} | Pattern repeats at least given number of times | | repeat(x, { min: n, max: n2 }) | x{n1,n2} | Pattern repeats between n1 and n2 number of times |

See Quantifiers API doc for more info.

Assertions

| Assertion | Regex Syntax | Description | | ------------------------- | ------------ | ------------------------------------------------------------------------ | | startOfString | ^ | Match the start of the string (or the start of a line in multiline mode) | | endOfString | $ | Match the end of the string (or the end of a line in multiline mode) | | wordBoundary | \b | Match the start or end of a word without consuming characters | | lookahead(...) | (?=...) | Match subsequent text without consuming it | | negativeLookahead(...) | (?!...) | Reject subsequent text without consuming it | | lookbehind(...) | (?<=...) | Match preceding text without consuming it | | negativeLookbehind(...) | (?<!...) | Reject preceding text without consuming it |

See Assertions API doc for more info.

Character classes

[!TIP] You may also use inline regexes for specifying character classes, as they offer a concise yet readable syntax. For example, /[a-z0-9_]/.

| Character class | Regex Syntax | Description | | --------------------- | ------------ | ------------------------------------------------- | | any | . | Any character | | word | \w | Word character: letter, digit, underscore | | digit | \d | Digit character: 0 to 9 | | whitespace | \s | Whitespace character: space, tab, line break, ... | | anyOf('abc') | [abc] | Any of provided characters | | charRange('a', 'z') | [a-z] | Character in a range | | charClass(...) | [...] | Union of multiple character classes | | negated(...) | [^...] | Negation of a given character class |

See Character Classes API doc and Unicode API doc for more info.

Examples

See Examples.

Performance

Regular expressions created with this library are executed at runtime, so you should avoid creating them in a context where they would need to be executed multiple times, e.g., inside loops or functions. We recommend that you create a top-level object for each required regex.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow. See the project guidelines to understand our core principles.

License

MIT

Inspiration

TS Regex Builder is inspired by Swift Regex Builder API.

Reference


Made with create-react-native-library