Split string once in javascript?

前端 未结 13 1011
南笙
南笙 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:46

    You can use:

    var splits = str.match(/([^|]*)\|(.*)/);
    splits.shift();
    

    The regex splits the string into two matching groups (parenthesized), the text preceding the first | and the text after. Then, we shift the result to get rid of the whole string match (splits[0]).

提交回复
热议问题