Read jsonRegExp string and return a RegExp for it
Implements jsonRegExp, e.g. reads in
'a+b'
and returns
/a\+b/
or
'`(`^`|/`).git`(`/`|`$`)'
and returns
/(^|\/)\.git(\/|$)/
hh lohmann <hh.lohmann@gmail.com>
Last 3 changes - see CHANGELOG file for full list and details
import { readJsonRegExp } from 'read-jsonregexp'
String: The string to interpret as jsonRegExp
none
RegExp represention for the evaluated jsonRegExp
const myString='k8s'
console.log(readJsonRegExp(myString)) // /k8s/
[]
const myString='k`[0-9`]s'
console.log(readJsonRegExp(myString)) // /k[0-9]s/
[]
const myString='k[number]s'
console.log(readJsonRegExp(myString)) // /k\[number\]s/
^
const myString='k`[^8`]s'
console.log(readJsonRegExp(myString)) // /k[\^8]s/
^
const myString='k`[`^8`]s'
console.log(readJsonRegExp(myString)) // /k[^8]s/
const myString='k`.s`/i'
console.log(readJsonRegExp(myString)) // /k.s/i
`/ contains anything that is not a valid flag (including multiple usage of the same flag) it will be given back as a literal sequence with backtick and slash, e.g.
const myString='k`.s`/noflags'
console.log(readJsonRegExp(myString)) // /k.s`\/noflags/
This substitutes (for various reasons) an error message like “invalid backtick sequence: sequence”
const myString='k``8s'
console.log(readJsonRegExp(myString)) // /k`8s/
Pick for your preferred package manager:
npm i read-jsonregexp
pnpm i read-jsonregexp
bun i read-jsonregexp
# For Yarn you should double check docs for your and / or
# current Yarn version, newer versions do not treat `i package_name`
# as an alias for `add ...` and exclude global installations
yarn add read-jsonregexp
^ $ \ * + ? { } ( ) [ ] |