A compiled-away, type-safe, readable RegExp alternative
magic-regexp is currently a work in progress.
- Runtime is zero-dependency and ultra-minimal
- Ships with transform for compiling runtime to pure RegExp
- Supports automatically typed capture groups
- Packed with useful utilities:
charIn,charNotIn,anyOf,char,word,digit,whitespace,letter,tab,linefeed,carriageReturn,not,maybe,exactly - All chainable with
and,or,after,before,notAfter,notBefore,times,as,at
Future ideas
- More TypeScript guard-rails
- More complex RegExp features/syntax
- Instrumentation for accurately getting coverage on RegExps
- Hybrid/partially-compiled RegExps for better dynamic support
Install package:
# npm
npm install magic-regexp
# yarn
yarn add magic-regexp
# pnpm
pnpm install magic-regexpimport { createRegExp, exactly } from 'magic-regexp'
const regExp = createRegExp(exactly('foo/test.js').after('bar/'))
console.log(regExp)
// /(?<=bar\/)foo\/test\.js/In order to statically transform magic-regexps at build time, you can use the included unplugin.
Nuxt:
import { defineNuxtConfig } from 'nuxt'
// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
// This will also enable auto-imports of magic-regexp helpers
modules: ['magic-regexp/nuxt'],
})Vite:
import { defineConfig } from 'vite'
import { MagicRegExpTransformPlugin } from 'magic-regexp'
export default defineConfig({
plugins: [MagicRegExpTransformPlugin.vite()],
})unbuild:
import { defineBuildConfig } from 'unbuild'
import { MagicRegExpTransformPlugin } from 'magic-regexp'
export default defineBuildConfig({
hooks: {
'rollup:options': (options, config) => {
config.plugins.push(MagicRegExpTransformPlugin.rollup())
},
},
})- Clone this repository
- Enable Corepack using
corepack enable(usenpm i -g corepackfor Node.js < 16.10) - Install dependencies using
pnpm install - Run interactive tests using
pnpm dev
Made with ❤️
Published under MIT License.