Search in VS Code, but \s is not equal to [ \f\n\r\t\v]

Viewed 256

I'm trying to replace extra blank after every line which matched like this

[\s]+\n

enter image description here

the problem is empty lines will also be matched

enter image description here

Base on my understanding, "\s" is equal to "[ \f\n\r\t\v]", so I changed the expression into

[ \f\r\t\v]+\n

to rule out "\n", but this time I got nothing...

enter image description here

Anybody knows why?

2 Answers

to find white space at ends of lines I would use

[ \t]+(?=\r?\n)

It will not catch the last line with white space if it has no \n. For that you need to use $ that is not affected by \n

[ \t]+$

Background

First of all, Visual Studio Code regex does not work in the usual way sometimes as it was "tweaked" to work with a text editor.

Here, note that \s only matches line break chars (vertical whitespace) if there is \n or \r in the pattern. So, [\s]+\n matches across lines, it matches 1 or more any whitespace chars, and then stops at the last newline in this chunk of whitespaces.

So, if you need to match horizontal whitespace at the end of a line, I thought you could use a combination of \s+ (one or more whitespace chars) and $ (end of a line).

However, \s+$ only matches whitespace at the end of a line if there are at least two occurrences of the whitespace chars.

Solution

\s*$

Demo screenshot:

enter image description here

Related