@brillout/part-regex
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published

Create a RegExp by defining some parts with strings and other parts with RegExp.

import partRegex from 'part-regex' // npm install part-regex

// Only match strings `/product/[0-9]+/edit`
const productEditPageUrl = partRegex`/product/${/[0-9]+/}/edit`
console.log(productEditPageUrl.test('/product/42/edit')) // Prints `true`

// Also supports a mix of String and RegEx variables.
// == Note how the first part is a RegEx and the second part is a string.
const userPageUrl = (isSettingsPage) => partRegex`/user/${/[a-zA-Z]+/}${isSettingsPage ? '/settings' : ''}`
console.log(userPageUrl(true).test('/user/brillout/settings')) // Prints `true`
console.log(userPageUrl(false).test('/user/turing')) // Prints `true`

Readme

Keywords

none

Package Sidebar

Install

npm i @brillout/part-regex

Weekly Downloads

1,435

Version

0.1.4

License

MIT

Unpacked Size

14.1 kB

Total Files

26

Last publish

Collaborators

  • brillout