Check if a string has white space

前端 未结 7 1571
青春惊慌失措
青春惊慌失措 2020-11-30 19:41

I\'m trying to check if a string has white space. I found this function but it doesn\'t seem to be working:

function hasWhiteSpace(s) 
{
            


        
7条回答
  •  暖寄归人
    2020-11-30 20:07

    You can simply use the indexOf method on the input string:

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

    Or you can use the test method, on a simple RegEx:

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

    This will also check for other white space characters like Tab.

提交回复
热议问题