How can I split a JavaScript string by white space or comma?

前端 未结 6 525
遥遥无期
遥遥无期 2020-12-12 15:10

If I try

\"my, tags are, in here\".split(\" ,\")

I get the following

[ \'my, tags are, in here\' ]

Wherea

6条回答
  •  既然无缘
    2020-12-12 16:08

    you can use regex in order to catch any length of white space, and this would be like:

    var text = "hoi how     are          you";
    var arr = text.split(/\s+/);
    
    console.log(arr) // will result : ["hoi", "how", "are", "you"]
    
    console.log(arr[2]) // will result : "are" 
    

提交回复
热议问题