0

This gives a null on Chrome or NodeJS:

console.log("a\r\nb".match(/a(.|\n)*b/));

The (.|\n)* should mean "any ASCII character including newline, and any number of them: 0 or more).

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • 1
    `.` does not match `\r`, CR, in JS regex. Never use `(.|\n)*`. Only `[^]` or `[\s\S]`, etc. – Wiktor Stribiżew Jan 29 '20 at 17:54
  • so `.` matches all ASCII characters except newline is not true either.... I am surprised somebody has `[^]` and `[\s\S]` at the tip of their tongue – nonopolarity Jan 29 '20 at 17:56
  • [More exactly](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes#Types), "Matches any single character except line terminators: `\n`, `\r`, `\u2028` or `\u2029`". – Álvaro González Jan 29 '20 at 17:59
  • that's why there is a saying... one mountain always higher than another mountain... but aren't I glad I am in CS... – nonopolarity Jan 29 '20 at 18:01
  • @nopole, I think something is lost in the translation – JoelFan Jan 29 '20 at 18:06
  • 1
    @JoelFan because there are people who knows `.` doesn't match a newline, and then there are people who thought `(.|\n)` will do the job, and then there are people who know it doesn't and `[^]` and `[\s\S]` does... and then there are people who know exactly `.` doesn't match newline but also `\r`, `\u2028` or `\u2029` – nonopolarity Jan 29 '20 at 20:25
  • It's about engines peculiarities e.g. `.` matches `\r` in PCRE by default (and the behavior is modifiable at runtime) but JS doesn't behave the same. Hence `any ASCII character including newline...` is not a precise definition. – revo Jan 29 '20 at 22:16
  • @revo `.` is any ASCII character excluding newline for PCRE and any ASCII character except `\n` and `\r` for JS then – nonopolarity Jan 29 '20 at 22:53
  • @nopole You're still missing `\u2028` and `\u2029`. I linked the exact definition from MDN, which is generallly a good source. – Álvaro González Jan 30 '20 at 10:12

0 Answers0