Split string once in javascript?

前端 未结 13 1001
南笙
南笙 2020-11-29 03:23

How can I split a string only once, i.e. make 1|Ceci n\'est pas une pipe: | Oui parse to: [\"1\", \"Ceci n\'est pas une pipe: | Oui\"]?

The

13条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 03:48

    You'd want to use String.indexOf('|') to get the index of the first occurrence of '|'.

    var i = s.indexOf('|');
    var splits = [s.slice(0,i), s.slice(i+1)];
    

提交回复
热议问题