Split string on the first white space occurrence

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

    I needed a slightly different result.

    I wanted the first word, and what ever came after it - even if it was blank.

    str.substr(0, text.indexOf(' ') == -1 ? text.length : text.indexOf(' '));
    str.substr(text.indexOf(' ') == -1 ? text.length : text.indexOf(' ') + 1);
    

    so if the input is oneword you get oneword and ''.

    If the input is one word and some more you get one and word and some more.

提交回复
热议问题