Comments (3)
Thank you for the fast response.
The first part, I completely agree with. Since groups
will always be undefined
if the regexp doesn't match.
Regarding the second case, you hit the nail on the head. I expect the value part of the type to be string | undefined
if and only if I mark at least one group as optional. Otherwise all the groups will always have a value, correct?
Edit: In the meantime I've started to write an enhancement request issue regarding the type resolution for the groups. It would be awesome if the type of each group could be inferred based on the anyOf
parameters.
from magic-regexp.
Hi @michaelschufi,
Yes, you are right. If all the groups are not optional, the match result should either be null
or matched array with all groups containing value. I think same goes for capturing groups too.
I've benn working on a new type inferrencing for a while, I've put more detail at #235, hopefully will resolve this and many other cases 👍
from magic-regexp.
Hi @michaelschufi,
I think the behavior you're observing is actually working as intended.
We didn't touch any runtime logic (helpers are transform to pure RegExp), so it behave exactly as native RegExp at runtime. The issue here is that /(?<opponent>A|B|C) (?<self>X|Y|Z)/
required both groups opponent
and self
to be matched, you can actually try with native RegExp:
console.log('B '.match(/(?<opponent>A|B|C) (?<self>X|Y|Z)/)?.groups);
// return `undefined`
maybe what you want/expect is making the self
part optional, you can try:
const regex = createRegExp(
anyOf('A', 'B', 'C')
.groupedAs('opponent')
.and(' ')
.and(anyOf('X', 'Y', 'Z').groupedAs('self').optionally())
);
this resolve to /(?<opponent>A|B|C) (?<self>X|Y|Z)?/
, this will still return undefined
if matching 'B ', as the self
groups is (or inside) a optional quantifier:
console.log('B '.match(/(?<opponent>A|B|C) (?<self>X|Y|Z)?/)?.groups)
// return `{ opponent: 'B', self: undefined }`
this is why the type of groups is Record<"opponent" | "self", string | undefined>, as we can still match a certain RegExp but not all of the groups having captured values.
Please let me know if you have any further questions or concerns. Thank you!
from magic-regexp.
Related Issues (20)
- $ Character gets escaped twice HOT 2
- `createRegExp` not sanitising strings (in type) HOT 1
- Very low contrast
- Incorrect open graph image HOT 1
- 500 - If you want to use `GithubLink` component, you must specify: `owner`, `repo` and `branch`.
- how do i recreate a password requirements regex? HOT 5
- No "exports" main defined in ....\.pnpm\[email protected]\node_modules\estree-walker\package.json HOT 3
- Lazy quantifier HOT 1
- not.word is not defined HOT 1
- Infer the literals used in the type of the groups HOT 10
- Allow for custom regex, and clarifying the usage of `anyOf()` HOT 7
- Library blocks Typescript compilation HOT 2
- Action Required: Fix Renovate Configuration
- Documentation site is down HOT 1
- Can I rework docs using Nuxt UI Pro HOT 4
- Maige Usage
- Support match everything in preset inputs
- `Predefined Input can not be assigned to InputSource` when using with TypeScript < 4.7 HOT 6
- performance testing HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from magic-regexp.