Matching an empty string with regex

前端 未结 5 867
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 20:21

I\'ve seen posts here on stackoverflow that say that the regex ^$ will match an empty string... So it made me think... why not something like this: ^\\s+$

5条回答
  •  春和景丽
    2020-12-20 20:29

    As others said, you probably mean ^\s*$, not ^\s+$, because ^\s+$ will fail to match the empty string, .

    Whether ^\s*$ matches an empty string depends on your definition of "empty". Like ^$, it will match the completely empty string . Unlike ^$, it will also match a string consistening of only whitespace characters like spaces and tabs, such as    . Which is the "right" definition of "empty" depends on the situation.

提交回复
热议问题