How do I remove trailing whitespace using a regula

2019-03-07 23:25发布

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

I tried:

\s+$

and:

([^\n]*)\s+\r\n

But they all removed empty lines too. I guess \s matches end-of-line characters too.


UPDATE (2016):

Nowadays I automate such code cleaning by using Sublime's TrailingSpaces package, with custom/user setting:

"trailing_spaces_trim_on_save": true

It highlights trailing white spaces and automatically trims them on save.

10条回答
贪生不怕死
2楼-- · 2019-03-07 23:34

In Java:



String str = "    hello world  ";

// prints "hello world" 
System.out.println(str.replaceAll("^(\\s+)|(\\s+)$", ""));


查看更多
我命由我不由天
3楼-- · 2019-03-07 23:36

You can use the regex /^[ \t\v\f]+\S.*$/gm to remove leading whitespace of each line, without touching blank lines nor trailing whitespace:

const str = ' X \n\nY Y\n \n Z'.replace(/^[ \t\v\f]+(\S.*)$/gm, '$1');
console.log(str);
// output: 'X \n\nY Y\n \nZ'

If you want more robust support although a bit slower, use the regex /^[ \t\v\f\xA0\uFEFF]+(\S.*)$/gm, which includes NBSP and BOM, making it closer to the \s behavior.

ADDED:

This is more simple and compliant with the \s behavior: /^(?=\s+).+(\S.*)$/gm ...but I don't know if it will work in older browsers.

WARNING: Any solution based on a simple regex can break ES6 TL strings.

查看更多
地球回转人心会变
4楼-- · 2019-03-07 23:46

If using Visual Studio 2012 and later (which uses .Net regular expressions), you can remove trailing whitespace without removing blank lines by using the following regex

Replace (?([^\r\n])\s)+(\r?\n)

With $1

enter image description here


Some explanation

The reason you need the rather complicated expression is that the character class \s matches spaces, tabs and newline characters, so \s+ will match a group of lines containing only whitespace. It doesn't help adding a $ termination to this regex, because this will still match a group of lines containing only whitespace and newline characters.

You may also want to know (as I did) exactly what the (?([^\r\n])\s) expression means. This is an Alternation Construct, which effectively means match to the whitespace character class if it is not a carriage return or linefeed.

Alternation constructs normally have a true and false part,

(?( expression ) yes | no )

but in this case the false part is not specified.

查看更多
SAY GOODBYE
5楼-- · 2019-03-07 23:46

To remove trailing white space while ignoring empty lines I use positive look-behind:

(?<=\S)\s+$

The look-behind is the way go to exclude the non-whitespace (\S) from the match.

查看更多
贪生不怕死
6楼-- · 2019-03-07 23:47

Try just removing trailing spaces and tabs:

[ \t]+$
查看更多
地球回转人心会变
7楼-- · 2019-03-07 23:49

The platform is not specified, but in C# (.NET) it would be:

Regular expression (presumes the multiline option - the example below uses it):

    [ \t]+(\r?$)

Replacement:

    $1

For an explanation of "\r?$", see Regular Expression Options, Multiline Mode (MSDN).

Full code example

This will remove all trailing spaces and all trailing TABs in all lines:

string cleanedUpText = Regex.Replace(inputText,
                                     @"[ \t]+(\r?$)", @"$1",
                                     RegexOptions.Multiline);
查看更多
登录 后发表回答