Frederick Zhang Frederick Zhang - 4 months ago 19
TypeScript Question

Different behaviour between `/REG/g` and `new RegExp()`?

I'm trying to match the components in a Rust compiler error output. This is the regexp I am using:

let newErrorRegex = /(warning|error|note|help)(?:\[(.*)\])?\: (.*)\s+--> (.*):(\d+):(\d+)\n(?:((?:.+\n)+)\.+)?(?:[\d\s]+\|.*)*((?:\s+=.*)+)?/g;

It works fine but it's too long, so I'd like to break it into multiple lines. Then I tried
new RegExp()

let newErrorRegex = new RegExp('(warning|error|note|help)(?:\[(.*)\])?\: (.*)\s+--> (.*):(\d+):(\d+)\n(?:((?:.+\n)+)\.+)?(?:[\d\s]+\|.*)*((?:\s+=.*)+)?', 'g');
const match = newErrorRegex.exec(output);

But it's not working this time! However some simpler instances have no such issues, e.g.,

let testRegexp = new RegExp('abc', 'g');
const match = testRegexp.exec('abcabc');

What's the difference between
new RegExp()
? How could I fix this issue? I have tried both TypeScript 1.8.7 and 2.0 (although I don't think it's related...).

Any help would be appreciated, thanks!

FYI, a sample of Rust compiler output could be:


In your first line you're using the syntax /foo/ that let the \ be what they are.

With new Regex('foo') you what to escape the backslashes because it's a string literal. Which leads to:

let newErrorRegex = new RegExp('(warning|error|note|help)(?:\\[(.*)\\])?\\: (.*)\\s+--> (.*):(\\d+):(\\d+)\\n(?:((?:.+\\n)+)\\.+)?(?:[\\d\\s]+\\|.*)*((?:\\s+=.*)+)?', 'g');