Match whitespace but not newlines

后端 未结 6 1470
忘掉有多难
忘掉有多难 2020-11-22 15:57

I sometimes want to match whitespace but not newline.

So far I\'ve been resorting to [ \\t]. Is there a less awkward way?

6条回答
  •  误落风尘
    2020-11-22 16:24

    A variation on Greg’s answer that includes carriage returns too:

    /[^\S\r\n]/
    

    This regex is safer than /[^\S\n]/ with no \r. My reasoning is that Windows uses \r\n for newlines, and Mac OS 9 used \r. You’re unlikely to find \r without \n nowadays, but if you do find it, it couldn’t mean anything but a newline. Thus, since \r can mean a newline, we should exclude it too.

提交回复
热议问题