Check if a single character is a whitespace?

后端 未结 9 1204
清酒与你
清酒与你 2021-02-06 21:21

What is the best way to check if a single character is a whitespace?

I know how to check this through a regex.

But I am not sure if this is the best way if I onl

9条回答
  •  粉色の甜心
    2021-02-06 21:49

    function hasWhiteSpace(s) {
      return /\s/g.test(s);
    }
    

    This will work

    or you can also use this indexOf():

    function hasWhiteSpace(s) {
      return s.indexOf(' ') >= 0;
    }
    

提交回复
热议问题