You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

typed-regex

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typed-regex

A typescript library for writing type-safe regular expressions using named capture groups

0.0.8
latest
Source
npmnpm
Version published
Weekly downloads
5.3K
28.59%
Maintainers
1
Weekly downloads
 
Created
Source

typed-regex

A typescript library for writing type-safe regular expressions using named capture groups.

npm npm bundle size

Install

To install the latest stable version of typed-regex -

yarn add typed-regex
// OR
npm install --save typed-regex

Usage

The type of the result object is infered from the regular expression.

import { TypedRegEx } from 'typed-regex';

const regex = TypedRegEx('^(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})$', 'g');
const result = regex.captures('2020-12-02');

result // : undefined | { year: string, month: string, day: string }

NOTE: The regular expression has to be a string literal for the types to be valid

Optional properties

If the capture group is marked as optional in the regular expression, the generated type will reflect that

const regex = TypedRegEx('(?<first>\\d+)/(?<second>\\w+)?', 'g');
const result = regex.captures('1234/foobar');

result // : undefined | { first: string, second?: string }

API Docs

You can find more information about the library in the API documentation

Browser support

Named capture groups are supported in these browser

License

Typed-Regex is licensed under MIT

Keywords

regex

FAQs

Package last updated on 13 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts