Check if a string has white space

前端 未结 7 1582
青春惊慌失措
青春惊慌失措 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条回答
  •  旧时难觅i
    2020-11-30 20:02

    One simple approach you could take is to compare the length of the original string with that of the string to have whitespaces replaced with nothing. For example:

    function hasWhiteSpaces(string) {
        if (string.length == string.replace(" ", "").length) {return false}
        return true
    }
    

提交回复
热议问题