Limiting the times that .split() splits, rather than truncating the resulting array

后端 未结 8 1550
耶瑟儿~
耶瑟儿~ 2020-12-11 01:58

Really, pretty much what the title says.

Say you have this string:

var theString = \"a=b=c=d\";

Now, when you run theString

8条回答
  •  孤街浪徒
    2020-12-11 02:50

    If you want to do that in less lines and avoiding loops:

    const theString = "some=string=with=separators";
    const limit = 2;
    const parts = theString.split('=', limit);
    parts.push(theString.slice(parts.join('').length + limit));
    

提交回复
热议问题