How can I determine if a string only contains spaces, using javascript?

前端 未结 5 1329
滥情空心
滥情空心 2021-02-05 23:24

How can I determine if an input string only contains spaces, using javascript?

5条回答
  •  半阙折子戏
    2021-02-05 23:54

    The fastest solution is using the regex prototype function test() and looking for any character that is not a space or a line break \S :

    if (/\S/.test(str))
    {
        // found something other than a space or a line break
    }
    

    In case that you have a super long string, it can make a significant difference.

提交回复
热议问题