I cannot figure a way to make regular expression match stop not on end of line, but on end of file in VS Code? Is it a tool limitation or there is some kind of pattern that I am not aware of?
82
votes
2 Answers
134
votes
It seems the CR is not matched with [\s\S]. Add \r to this character class:
[\s\S\r]+
will match any 1+ chars.
Other alternatives that proved working are [^\r]+ and [\w\W]+.
If you want to make any character class match line breaks, be it a positive or negative character class, you need to add \r in it.
Examples:
- Any text between the two closest
aandbchars:a[^ab\r]*b - Any text between
STARTand the closestSTOPwords:START[\s\S\r]*?STOPSTART[^\r]*?STOPSTART[\w\W]*?STOP
- Any text between the closest
STARTandSTOPwords:START(?:(?!START)[\s\S\r])*?STOP
See a demo screenshot below:

[\s\S]work? - Wiktor Stribiżew*quantifier it stops on EOL - Dima Ogurtsov