read-jsonregexp
v1.0.0
Published
Read jsonRegExp string and return a RegExp for it
Readme
readJsonRegExp
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 <[email protected]>
Changelog
Last 3 changes - see CHANGELOG file for full list and details
- (1.0.0) v1.0.0
Synopsis
import { readJsonRegExp } from 'read-jsonregexp'Parameters
jsonRegExp
String: The string to interpret as jsonRegExp
Settings
none
Returns
RegExp represention for the evaluated jsonRegExp
Examples
jsonRegExp without any RegExp
const myString='k8s' console.log(readJsonRegExp(myString)) // /k8s/jsonRegExp with RegExp for character range with RegExp metacharacters
[]const myString='k`[0-9`]s' console.log(readJsonRegExp(myString)) // /k[0-9]s/jsonRegExp with literal
[]const myString='k[number]s' console.log(readJsonRegExp(myString)) // /k\[number\]s/jsonRegExp with RegExp for character range including literal
^const myString='k`[^8`]s' console.log(readJsonRegExp(myString)) // /k[\^8]s/jsonRegExp with RegExp for character range negated by RegExp metacharacter
^const myString='k`[`^8`]s' console.log(readJsonRegExp(myString)) // /k[^8]s/jsonRegExp with RegExp with flag
const myString='k`.s`/i' console.log(readJsonRegExp(myString)) // /k.s/i- if the part after `/ 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.
This substitutes (for various reasons) an error message like "invalid backtick sequence:const myString='k`.s`/noflags' console.log(readJsonRegExp(myString)) // /k.s`\/noflags/sequence"
- if the part after `/ 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.
jsonRegExp with literal backtick (= backtick quoted by backtick)
const myString='k``8s' console.log(readJsonRegExp(myString)) // /k`8s/
Installation
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-jsonregexpDetails
- Expects a single string as input, not a JSON object or file (i.e. a corresponding JSON has to be read in / evaluated by other means, e.g. a process that uses readJsonRegExp to transform a JSON into a JavaScript object that allows RegExps as values)
- Uses JavaScript's RegExp to evaluate and transform jsonRegExps
- Handles the RegExp metacharacters recognized by JavaScript's RegExp:
^ $ \ * + ? { } ( ) [ ] |
Tests
- Code tests to be run with Node.js / Bun available in Source Code
Source Code
License
- MIT (see LICENSE.txt)
References
hh lohmann: jsonRegExp
- Store RegExp as JSON string without an unreadable mess of escaping / quoting metacharacters
- https://hh-lohmann.github.io/json-regexp/
