正则表达式分裂字符串与两个分隔符

时间:2016-10-01 18:26:35

标签: javascript regex split match separator

使用JavaScript,有没有办法将字符串拆分为带有两个分隔符的数组:':'和','

对于var str =" 21:223,310:320&#34 ;;

希望结果为:[21,223,310,320];

谢谢!

2 个答案:

答案 0 :(得分:3)

您可以使用正则表达式查找:或使用可选空格,的逗号。



console.log("21:223, 310:320,42".split(/:|, */));




答案 1 :(得分:1)

如果您的表达式与此match

相似,则可以使用"21:223, 310:320"



var str = "21 :  223 , 310 :  320 ";
//---------^^----^^^---^^^----^^^--
// group of digits(represented by ^) will be matched
console.log(str.match(/(\d+)/g));
// will return ["21", "223", "310", "320"]




相关问题