regex to remove all whitespaces except between brackets

后端 未结 6 2093
我在风中等你
我在风中等你 2021-01-06 13:01

I\'ve been wrestling with an issue I was hoping to solve with regex.

Let\'s say I have a string that can contain any alphanumeric with the possibility of a substrin

6条回答
  •  粉色の甜心
    2021-01-06 13:59

    The following will match start-of-line or end-of-bracket (which must come before any space you want to match) followed by anything that isn't start-of-bracket or a space, followed by some space.

    /((^|\])[^ \[]*) +/
    

    replacing "all" with $1 will remove the first block of spaces from each non-bracketed sequence. You will have to repeat the match to remove all spaces.

    Example:

    abcd efg [hij klm]nop qrst u
    abcdefg [hij klm]nopqrst u
    abcdefg[hij klm]nopqrstu
    done
    

提交回复
热议问题