Split string on the first white space occurrence

后端 未结 13 2086
孤街浪徒
孤街浪徒 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:55

    Late to the game, I know but there seems to be a very simple way to do this:

    const str = "72 tocirah sneab";
    const arr = str.split(/ (.*)/);
    console.log(arr);

    This will leave arr[0] with "72" and arr[1] with "tocirah sneab". Note that arr[2] will be empty, but you can just ignore it.

    For reference:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split#Capturing_parentheses

提交回复
热议问题