How do I split a string, breaking at a particular character?

后端 未结 17 2443
误落风尘
误落风尘 2020-11-21 05:07

I have this string

\'john smith~123 Street~Apt 4~New York~NY~12345\'

Using JavaScript, what is the fastest way to parse this into



        
17条回答
  •  失恋的感觉
    2020-11-21 05:21

    Since the splitting on commas question is duplicated to this question, adding this here.

    If you want to split on a character and also handle extra whitespace that might follow that character, which often happens with commas, you can use replace then split, like this:

    var items = string.replace(/,\s+/, ",").split(',')
    

提交回复
热议问题