Type named capture groups better
See original GitHub issueSearch Terms
named regexp, named capture groups
Motivation
Currently named capture groups are a bit of a pain in TypeScript:
- All names are of type string even if the regexp doesn’t have that named group.
- You need to use non-null assertion for
.groups
even when that is the only possibility.
Suggestion
I propose making RegExp
higher order on its named capture groups so that .groups
is well typed.
// Would have type: RegExp<{ year: string, month: string }>
const date = /(?<year>[0-9]{4})-(?<month>[0-9]{2})/
const match = someString.match(date)
if (match) {
// match.groups type would be { year: string, month: string }
// currently is undefined | { [key: string]: string }
}
// Would have type RegExp<{ year: string, month?: string }>
const optionalMonth = /(?<year>[0-9]{4})(-(?<month>[0-9]{2}))?/
Checklist
My suggestion meets these guidelines:
- [✓] This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- [✓] This wouldn’t change the runtime behavior of existing JavaScript code
- [✓] This could be implemented without emitting different JS based on the types of the expressions
- [✓] This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- [✓] This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:49
- Comments:15 (4 by maintainers)
Top Results From Across the Web
Groups and backreferences - JavaScript - MDN Web Docs
Named capturing group : Matches "x" and stores it on the groups property of the returned matches under the name specified by <Name>...
Read more >New JavaScript Features That Will Change How You Write ...
Named capture groups use a more expressive syntax compared to regular capture groups. The s ( dotAll ) flag changes the behavior of...
Read more >Named capturing groups in JavaScript regex? - Stack Overflow
There are only two "structural" advantages of named capturing groups I can think of: In some regex flavors (.NET and JGSoft, as far...
Read more >Groups & Capturing Groups - Regular Expressions Basics
Use (?: ) for non capturing groups. If you need to use a group as a block but you won't process the results...
Read more >Grouping Constructs in Regular Expressions - Microsoft Learn
A capturing group has a default name that is identical to its ordinal number. For more information, see Named matched subexpressions later ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For everyone that wants to have type safety and auto completion on the
groups
part right now you can declare the variable the match result is stored in as theRegExpMatchArrayWithGroups
type below like the following:The
RegExpMatchArrayWithGroups
type needs a type argument where all the group names in the regex are duplicated, so there is no automatic parsing from it, but when things are in the same statement this should be quite maintainable.Definitions:
Thanks @hlovdal. I took your idea made it easier to use.
Playground