Check if a single character is a whitespace?

后端 未结 9 1199
清酒与你
清酒与你 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:55

    If you only want to test for certain whitespace characters, do so manually, otherwise, use a regular expression, ie

    /\s/.test(ch)
    

    Keep in mind that different browsers match different characters, eg in Firefox, \s is equivalent to (source)

    [ \f\n\r\t\v\u00A0\u2028\u2029]
    

    whereas in Internet Explorer, it should be (source)

    [ \f\n\r\t\v]
    

    The MSDN page actually forgot the space ;)

提交回复
热议问题