Skip to content

Invalid regular expression (re2) [\s-_] #698

@zbysir

Description

@zbysir

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:

    1. replace [\s-_] with [\s\-_] (in parser.TransformRegExp),
    1. ignore this error and use reg2.

But I don't know how to integrate better into goja without causing other problems.

Need help.

Reference:

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