Difference between .split(/\s+/) and .split(“ ”)?

前端 未结 3 1883
伪装坚强ぢ
伪装坚强ぢ 2021-02-15 11:22

:) First of all, sorry my bady english :p I was taking a look to the next js code fragment:

var classes = element.className.split(/\\s+/);

That c

3条回答
  •  礼貌的吻别
    2021-02-15 11:48

    No, .split(/\s+/), and .split(" ") are different ones. \s+ matches one or more space characters including line breaks where " " matches a single horizontal space character. So .split(/\s+/) splits the input according to one or more space characters and .split(" ") splits the input according to a single space.

    Example:

    > "foo   bar".split(/\s+/)
    [ 'foo', 'bar' ]
    > "foo   bar".split(" ")
    [ 'foo', '', '', 'bar' ]
    

提交回复
热议问题