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

前端 未结 3 1881
伪装坚强ぢ
伪装坚强ぢ 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:46

    The difference between .split(" ") and .split(/\s+/) is:

    The regex " "

    • Match the space character literally.

    The regex /\s+/

    • Match a single whitespacecharacter (tab, line feed, carriage return, vertical tab, form feed) between one and unlimmited times. (greedy)

    Short:

    " "   splits the array at one single space character.
    /\s/ splits the array at every kind of whitespace character
    +      Matches between one and unlimitted times

提交回复
热议问题