How do I remove trailing whitespace using a regular expression?

后端 未结 10 2103
无人及你
无人及你 2021-01-30 04:00

I want to remove trailing white spaces and tabs from my code without removing empty lines.

I tried:

\\s+$

and:

([^\\n]         


        
10条回答
  •  面向向阳花
    2021-01-30 04:20

    To remove trailing whitespace while also preserving whitespace-only lines, you want the regex to only remove trailing whitespace after non-whitespace characters. So you need to first check for a non-whitespace character. This means that the non-whitespace character will be included in the match, so you need to include it in the replacement.

    Regex: ([^ \t\r\n])[ \t]+$

    Replacement: \1 or $1, depending on the IDE

提交回复
热议问题