unjs / magic-regexp Goto Github PK
View Code? Open in Web Editor NEWA compiled-away, type-safe, readable RegExp alternative
Home Page: https://regexp.dev
License: MIT License
A compiled-away, type-safe, readable RegExp alternative
Home Page: https://regexp.dev
License: MIT License
When I try to use magic-regexp
in a Laravel + Vite project I get the following errors:
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/hassan/project/node_modules/.pnpm/[email protected][email protected]/node_modules/magic-regexp/dist/transform.mjs not supported.
You can easily reproduce by following these steps:
laraven new
npx @preset/cli apply laravel:inertia
php artisan serve
npm run dev
When you add the vite plugin, you will see the dev
command will fail with error.
I expect it to work based on the project readme instructions and code examples.
No response
In the docs it is written not
can be prefixed before word, wordChar, wordBoundary, digit, whitespace, letter, letter.lowercase, letter.uppercase, tab, linefeed or carriageReturn.
But when i used it in my javascript project it returns undefined
Steps to reproduce:
index.js
npm init -y
//const regexp = createRegExp(not.word.times); <- error
console.log(Object.keys(not).includes('word')) //false
console.log(Object.keys(not).includes('wordChar')) //true
https://stackblitz.com/edit/github-cr7ofh?file=index.mjs
Expected that not.word would not be undefined as it was in the docs
No response
Is it possible to use this library and create the lazy quantifier?
For example I would like to get such regex: (?:\w|-)+?
. The main part I'm asking here is the +?
.
No response
No response
How to create typed function with RegExp or MagicRegExp input?
I write something like
function getMatchedResult<R extends MagicRegExp<any, any, any, any> | RegExp>(regexp: R, str: string) {
return str.match(regexp);
}
it doesn't matter Magic Regexp or just Regexp you place the function ReturnType
is RegExpMatchArray | null
And is it possible not to use magic regexp as a dependency but to support it? (for example maybe implement compability types)
I am the maintainer of the Telegram Bot API framework - GramIO
And if i add support magic-regexp it would be amazing
I need to allow magic (with type-safety) and not regexp
No response
No response
Hello,
Current docs is built with docus which is currently deprecated. Nitro and H3 will use Nuxt UI Pro in a near future. Can I do the same for the current lib?
No response
No response
right now the function letter
is include all the uppercase and lowercase letter, my use case is i only need the lowercase letter, and uppercase letter is prohibited.
No response
No response
Visiting https://regexp.dev/ returns a 500: INTERNAL_SERVER_ERROR
, preventing anyone from accessing the really important documentation.
The website should load.
No response
Thanks for trying Maige.
Running GPT-based services is pricey. At this point, we ask you to add payment info to continue using Maige.
Feel free to close this issue.
Error trying import ./build.config from /workspaces/Optc Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /workspaces/Optc/node_modules/.pnpm/[email protected][email protected]/node_modules/estree-walker/package.json
at new NodeError (node:internal/errors:371:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:453:9)
at packageExportsResolve (node:internal/modules/esm/resolve:671:7)
at resolveExports (node:internal/modules/cjs/loader:482:36)
at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.resolve (node:internal/modules/cjs/helpers:108:19)
at _resolve (/workspaces/Optc/node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.js:1:108226)
at jiti (/workspaces/Optc/node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.js:1:110413)
at /workspaces/Optc/node_modules/.pnpm/[email protected][email protected]/node_modules/magic-regexp/dist/transform.mjs:3:21 {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
https://stackblitz.com/edit/github-dpu8mc?file=build.config.ts
It should build.
An issue about estree-walker: Rich-Harris/estree-walker#26
e.g. /./g.test('thing')
https://regexp.dev/getting-started/setup
Please, make contrast in code section more readable.
No response
Suggestion
You should add macroability and predefine some macros like http link, xml tag or i don't know maybe uk phone macros etc.
https://twitter.com/senbanabakhele/status/1548715207194824706
More examples:
No response
No response
with the amount of inference we're doing, it would be a great enhancement to add some kind of type performance test to ensure that end users' TS checkers or IDEs don't get slower. With metrics, we also will have a basis for making performance improvements.
createRegExp('.com')
should return the same type as createRegExp(exactly('.com'))
Depends on strongly typing anonymous groups: #24
500
Failed to fetch dynamically imported module: https://magic-regexp.roe.dev/_nuxt/fluid-d2d9e9d9.mjs
load the homepage
ublock origin
After running pnpm install and adding to vite.config.ts, I got the following error. I'm using vite 4 and pnpm
failed to load config from C:\Git\BlocPlatform\Bloc\Frontend\Bloc.Vue\vite.config.ts
error when starting dev server:
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in C:\Git\BlocPlatform\Bloc\Frontend\Bloc.Vue\node_modules\.pnpm\[email protected]\node_modules\estree-walker\package.json
at new NodeError (node:internal/errors:372:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:472:9)
at packageExportsResolve (node:internal/modules/esm/resolve:693:7)
at resolveExports (node:internal/modules/cjs/loader:482:36)
at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (C:\Git\BlocPlatform\Bloc\Frontend\Bloc.Vue\node_modules\.pnpm\[email protected]\node_modules\magic-regexp\dist\transform.cjs:5:22)
* The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command npm run dev" terminated with exit code: 1.
* Terminal will be reused by tasks, press any key to close it.
n/a
Expected config to load.
No response
If you share the url https://regexp.dev/
it shows a completely different open graph image of Docus
instead of the library.
Just send https://regexp.dev/
on discord or twitter
No response
@didavid61202 @danielroe , As I was working on issue #7, two issues/points of clarification for the API came to mind.
Unless there is a function that I don't know of, a user can't add to an expression using custom regex unless it is exported as one of the helpers for specific RegExp characters such as digit
, whitespace
, letter
, etc. For example, using the current API there is no way to include the following regex pattern [1-9]
without it being passed to exactly()
and ending up as \[1-9\]
.
import { exactly } from "magic-regexp";
const test = exactly("foo").and("[1-9]"); // foo\[1-9\]
Note: the regex pattern was passed to exactly()
here
When working within the package, we can create these arbitrary regex patterns using createInput()
; however, this function isn't exported to end users.
createInput
named one of the following input
, rawInput
,regex
, or some other suggestionProviding an alias to createInput
would allow for patterns such as:
import { exactly, input } from "magic-regexp";
const test = exactly("foo").and(input("[1-9]")); // foo[1-9]
anyOf
@didavid61202 @danielroe, the anyOf
function states that it takes an array of inputs, but it doesn't really take an array; it takes an arbitrary number of arguments. The function documentation stating it takes an array can lead to confusion, and in fact, it confused me when I first started using this package.
Consider the following examples:
import { anyOf } from "magic-regexp";
const test1 = anyOf(...["a", "b", "c"]); //(?:a|b|c)
const test2 = anyOf("a", "b", "c"); //(?:a|b|c)
const test3 = anyOf(["a", "b", "c"]); //(?:a,b,c)
const test4 = anyOf("abc"); //(?:abc)
creating project documentation, documentation even at the beginning of the project is important for more people to test and give more ideas and get feedback from the community.
Vitepress, is powerful and can be easily implemented.
No response
No response
i'm trying to recreate a password requirements regex like the following:
password must contains al least 8 characters, one lowercase character, one uppercase char, one digit and one special character (! @ # $ % ^ & *)
const PASSWORD_RE= createRegExp(
(
oneOrMore(letter.lowercase).times.atLeast(1)
.and(oneOrMore(letter.uppercase).times.atLeast(1))
.and(oneOrMore(digit))
.and(
oneOrMore(
charIn('!')
.or(charIn('@'))
.or(charIn('#'))
.or(charIn('$'))
.or(charIn('%'))
.or(charIn('^'))
.or(charIn('&'))
.or(charIn('*'))
).times.atLeast(1)
)
)
.times.atLeast(8),['g']
)
i tried to paste the generated regex in one of the regex tester like regex101
but seems to not working as expected
the documentation seems lacking in terms of providing a fully understandable examples
can you help me to achieve a working password regex? Where am I doing wrong?
No response
No response
I saw this example on the documentation.
import { createRegExp, exactly } from 'magic-regexp'
const regExp = createRegExp(exactly('foo/test.js').after('bar/'))
console.log(regExp)
// /(?<=bar\/)foo\/test\.js/
This example can work with safari?
I just installed magic-regexp via yarn add magic-regexp
and tried the first example from https://regexp.dev/getting-started/examples
Typescript does not like it though: Argument of type 'Input<"\\d", never>' is not assignable to parameter of type 'InputSource<string, never>'.
The code can also be simplified even more:
const regex = createRegExp(oneOrMore(digit));
I have created a codesandbox for showcasing this: https://codesandbox.io/s/gracious-swartz-3sdfcs
Navigating to an explicit URL like https://regexp.dev/getting-started/examples provides a 500 error.
https://regexp.dev/getting-started/examples
Loads the proper page.
No response
When Trying to build a Typescript Project using magic-regexp tsc hangs indefinitely.
It just hangs and does nothing.
The Problem occurred in different projects (finding the problem the first time did cost me a good amount of time).
https://stackblitz.com/edit/github-dmq6xe?file=package.json
The build Process should go through or at least throw an error.
When using ts-node to run the Script it works for whatever reason.
I've added a build
script & a start:ts-node
script to the example.
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates are awaiting their schedule. Click on a checkbox to get an update now.
@vitest/coverage-v8
, vitest
)These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.
.github/workflows/bench.yml
actions/checkout v4
pnpm/action-setup v2
actions/setup-node v4
CodSpeedHQ/action v2
.github/workflows/ci.yml
actions/checkout v4@a5ac7e51b41094c92402da3b24376905380afc29
actions/setup-node v4
actions/checkout v4@a5ac7e51b41094c92402da3b24376905380afc29
actions/setup-node v4
codecov/codecov-action v4
.github/workflows/codeql.yml
actions/checkout v4@a5ac7e51b41094c92402da3b24376905380afc29
github/codeql-action v3
github/codeql-action v3
github/codeql-action v3
.github/workflows/performance.yml
actions/checkout v4@a5ac7e51b41094c92402da3b24376905380afc29
actions/setup-node v4
beerose/tsc-diagnostics-diff-action 49bf67cb35cae2ba688d1e7dffe5f14acbb62406
.github/workflows/release.yml
actions/checkout v4@a5ac7e51b41094c92402da3b24376905380afc29
actions/setup-node v4
docs/package.json
undocs 0.2.20
package.json
estree-walker ^3.0.3
magic-string ^0.30.8
mlly ^1.6.1
regexp-tree ^0.1.27
type-level-regexp ~0.1.17
ufo ^1.4.0
unplugin ^1.8.3
@antfu/eslint-config 2.19.0
@codspeed/vitest-plugin 3.1.0
@nuxt/kit 3.10.3
@types/estree 1.0.5
@types/node 20.11.30
@vitest/coverage-v8 1.3.1
acorn 8.11.3
bumpp 9.4.1
eslint 9.3.0
expect-type 0.18.0
rollup 4.12.1
simple-git-hooks 2.10.0
typescript 5.4.5
unbuild 2.0.0
vite 5.2.11
vitest 1.3.1
pnpm 8.15.8
playground/package.json
When I create a regexp group using anyOf
and groupedAs
, the type of the matched group should be inferred based on the inputs to anyOf
.
Similar to the type of the main regexp that gets inferred.
E.g.
const regex = createRegExp(
anyOf("A", "B", "C")
.groupedAs("opponent")
.and(" ")
.and(anyOf("X", "Y", "Z").groupedAs("self")),
);
Here, the type of regex
is
MagicRegExp<"/(?<opponent>A|B|C) (?<self>X|Y|Z)/", "opponent" | "self", ["(?<opponent>A|B|C)", ...any[]], never>
so the information that the groups have literals of ABC and XYZ respectively is included in the type. However, the type of
"A Y".match(regex)?.groups.opponent
is string | undefined
instead of "A" | "B" | "C"
.
No response
My goal is to use this library in conjunction with colinhacks/zod. magic-regexp complements zod well when it comes to string validation and parsing while still being typesafe.
I'm open to work on this myself. Please let me know where I should have a look to get started with such a feature.
Hi
First, thank you so much for this library. I love it so far. I came across a typing issue while trying it out, and thought I'd report it.
const regex = createRegExp(
anyOf('A', 'B', 'C')
.groupedAs('opponent')
.and(' ')
.and(anyOf('X', 'Y', 'Z').groupedAs('self'))
);
// Results in: /(?<opponent>A|B|C) (?<self>X|Y|Z)/
console.log('A Y'.match(regex)?.groups); // { opponent: 'A', self: 'Y' }
console.log('B '.match(regex)?.groups); // undefined
console.log('C Z'.match(regex)?.groups); // { opponent: 'C', self: 'Z' }
The type of each groups
is Record<"opponent" | "self", string | undefined>
.
Why is the type in
https://github.com/danielroe/magic-regexp/blob/50ac0caa55e6e3fe2c7397297d8a1533190c5012/src/core/types/magic-regexp.ts#L35
defined as string | undefined
?
How can the value of the group be undefined while still matching the reg exp?
https://stackblitz.com/edit/github-ppjiny?file=index.ts
The type of each groups
should be Record<"opponent" | "self", string>
.
No response
There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.
Error type: Cannot find preset's package (:automergeEarlyMondays). Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.
exactly('$')
gets compiled to \\$
, it should be just \$
https://stackblitz.com/edit/github-rujd5h?file=package-lock.json,index.mjs
exactly('$')
should compile to \$
No response
There are lot of regex being used in codebase. And if there is an easy way to put them in playground and get the equivalent of it in magic-regex - it will be awesome
There are lot of regex being used in codebase. And if there is an easy way to put them in playground and get the equivalent of it in magic-regex - it will be awesome
No response
No response
I want to match all characters within a script
tag, but since there isn't a preset input to match everything, so I have to manually write ([\s\S]*?)
for match the code.
Maybe an export named every
can be added to preset inputs, or export createInput
to allow for custom input definitions could be a solution ?
No response
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.