字符串由两个不同的分隔符分隔

时间:2023-01-11 21:47:36

I have strings like this 'some-dasd\dasd-dasdas\dasdas-dasd-das\dsad'. I need to split the string to array by two different symbols '\' and '-', so I would like to get the array ['some', 'dasd', 'dasd', 'dasdas', 'dasdas', 'dasd', 'das' ,'dsad'].

我有这样的字符串'some-dasd \ dasd-dasdas \ dasdas-dasd-das \ dsad'。我需要将字符串分成两个不同的符号'​​\'和' - ',所以我想得到数组['some','dasd','dasd','dasdas','dasdas',' dasd','das','dsad']。

What is the best way to do it?

最好的方法是什么?

2 个解决方案

#1


14  

"ome-dasd\dasd-dasdas\dasdas-dasd-das\dsad".split(/\\|-/)

should do the trick.

应该做的伎俩。

#2


11  

You can split string using regexp:

您可以使用regexp拆分字符串:

 mystring.split(/[-\\]/)

#1


14  

"ome-dasd\dasd-dasdas\dasdas-dasd-das\dsad".split(/\\|-/)

should do the trick.

应该做的伎俩。

#2


11  

You can split string using regexp:

您可以使用regexp拆分字符串:

 mystring.split(/[-\\]/)