-1

I was trying to detect incorrectly (over-)intended code like that

foo {
  bar {
      baz {

with regular expressions and I found an interesting behavior.

The regex I'm using is /^( *).+\n\1 {3,}/ and in VS Code (standard find&replace dialog) it triggers false-positives on everything. At the same time in JavaScript (Chrome 74) there's no such behavior.

The stricter regex /^( *)\S.+\n\1 {3,}/ works correctly, but I wonder why does the difference exist? Is it that in JavaScript does not backtrack after line break (this is my wild guess) or does VS Code handle whitespace characters specifically making e.g. matching them ungreedy by default?

PS: IE works the same way as Chrome.

kirilloid
  • 14,011
  • 6
  • 38
  • 52
  • I cannot repro, please describe how you tested the regex and post the *string literal* you tested the regex against - it is not clear if there are any CR chars or not before LF. – Wiktor Stribiżew Aug 13 '19 at 10:53
  • [Doesn't work in regex101](https://regex101.com/r/F3hYvT/1). I added a `\r?` just in case but there was no difference without it. Tried all flavours of regex on the site. I don't have VS Code to check. – VLAZ Aug 13 '19 at 10:55
  • @VLAZ There is no point adding `\r?` at regex101.com: it does not allow the CR chars in the test strings. All linebreaks are LF only there. – Wiktor Stribiżew Aug 13 '19 at 11:04
  • @WiktorStribiżew ah, I didn't know that. Thanks. – VLAZ Aug 13 '19 at 11:05
  • @VLAZ works on that site with `m` flag the same way as in JS and VSCode – kirilloid Aug 13 '19 at 14:30
  • @kirilloid damn, I thought I had that flag on. Oh well... – VLAZ Aug 13 '19 at 21:32

1 Answers1

0

Ok, it seems that VSCode runs regular expression with flags im. Without m flag, ^ matches only the very beginning of a string—not a start of any new line.

And BTW JS doesn't support \A and \Z anchors;

kirilloid
  • 14,011
  • 6
  • 38
  • 52