Skip to content

Regex named group support #659

@bric3

Description

@bric3

I was playing with a tool using goja, and noticed that regex using groups didn't work.

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Named_capturing_group

const g = /^(?<timestamp>\d+),(?<author>.+)$/.exec(
   "1560979912,Caroline",
).groups

I believe the current implementation is incomplete in that front, the groups isn't even present.
Looking at the code it seems that both methods (UTF-16 and Unicode) are constructing only an array of the groups.

Seen in findSubmatchIndexUTF16 and findSubmatchIndexUnicode in regexp.go.

	groups := match.Groups()

	result = make([]int, 0, len(groups)<<1)
	for _, group := range groups {
		if len(group.Captures) > 0 {
			result = append(result, posMap[group.Index], posMap[group.Index+group.Length])
		} else {
			result = append(result, -1, 0)
		}
	}

I know that https://github.com/dlclark/regexp2 supports name groups.

I understand the nature of groups itself is "strange"

A null-prototype object of named capturing groups, whose keys are the names, and values are the capturing groups, or undefined if no named capturing groups were defined. See capturing groups for more information.

That said, maybe the current implementation can be revisited to support those ? Maybe .groups can be intercepted in place to report those ?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions