Split string on the first white space occurrence

后端 未结 13 2012
孤街浪徒
孤街浪徒 2020-11-28 20:07

I didn\'t get an optimized regex that split me a String basing into the first white space occurrence:

var str=\"72 tocirah sneab\";

I need

13条回答
  •  醉梦人生
    2020-11-28 20:41

    var arr = [];             //new storage
    str = str.split(' ');     //split by spaces
    arr.push(str.shift());    //add the number
    arr.push(str.join(' '));  //and the rest of the string
    
    //arr is now:
    ["72","tocirah sneab"];
    

    but i still think there is a faster way though.

提交回复
热议问题