2

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 /REG/g and 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: https://regex101.com/r/vKFWYW/1

Frederick Zhang
  • 3,593
  • 4
  • 32
  • 54

1 Answers1

3

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');
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142