duktape: Regular expression parse error - /^\_/ and /\\{/g

Hi, I have some tests for regular expression.

case 1

var s = "abcd";
s.search(/^\_/);

SyntaxError: invalid regexp escape (line 1)
    duk_lexer.c:1578

case 2

var s = "abcd";
s.search(/\\{/g);

SyntaxError: invalid regexp quantifier (unknown char) (line 1)
    duk_lexer.c:1468

In the regular expression, I think that '' has some problem.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 17 (9 by maintainers)

Most upvoted comments

The Ecmascript specification is quite strict in what escapes are allowed and required. In your case:

  • Case 1: it’s not valid to escape an underscore, so the correct form would be /^_/, without the underscore.
  • Case 2: an open curly brace begins a quantifier of the form {n} or {n,m}. If you want to match a literal open curly brace, it must be escaped: /\\\{/.

Most Ecmascript engines are rather loose about regexp syntax, accepting many regexps that are technically syntax errors in the specification (as far as I’ve been able to determine). Because this is common behavior, Duktape may need to move into that direction, but right now it follows the E5 specification quite strictly.