Search code examples
regexhaxe

"Invalid Escape Sequence" with regex in Haxe


How can I get Haxe to match parentheses in regular expressions?

I always get the error Invalid Escape Sequence.

var reg = new EReg('\([0-9]+\)', 'i'); // Throws error
reg.match('(9)');
trace(reg.matched(0));

http://try.haxe.org/#345D6


Solution

  • In strings, you need to escape the \ character using \\. The following works:

    var reg = new EReg('\\([0-9]+\\)', 'i');
    

    Alternatively, Haxe has regex literals you can use here:

    var reg = ~/\([0-9]+\)/i;