Check if a string has white space

前端 未结 7 1561
青春惊慌失措
青春惊慌失措 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 19:58

    Your regex won't match anything, as it is. You definitely need to remove the quotes -- the "/" characters are sufficient.

    /^\s+$/ is checking whether the string is ALL whitespace:

    • ^ matches the start of the string.
    • \s+ means at least 1, possibly more, spaces.
    • $ matches the end of the string.

    Try replacing the regex with /\s/ (and no quotes)

提交回复
热议问题