-
Notifications
You must be signed in to change notification settings - Fork 435
Open
Description
Run the code below:
func TestGoJaReg(t *testing.T) {
vm := goja.New()
_, err := vm.RunString(`const toCamelCase = (string) => string.replace(
/^([A-Z])|[\s-_]+(\w)/g,
(match, p1, p2) => p2 ? p2.toUpperCase() : p1.toLowerCase()
);`)
if err != nil {
t.Fatalf("run string error: %v", err)
}
}
Will get
run string error: SyntaxError: SyntaxError: Invalid regular expression (re2): ^([A-Z])|[
�
-_]+(\w) (error parsing regexp: invalid character class range: `-_`) at 2:3
These are some reasons I found:
[\s-_] is the correct way to write it in JavaScript, but in Go, - will be recognized as a range operator, and I tried two ways to solve this problem:
-
- replace
[\s-_]with[\s\-_](in parser.TransformRegExp),
- replace
-
- ignore this error and use reg2.
But I don't know how to integrate better into goja without causing other problems.
Need help.
Reference:
- Another associated issue, maybe useful: Valid regexp panics instead of falling back to
regexp2#681
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels