带有两个分隔符的正则表达式拆分字符串 [英] regex split string with two separators

查看:87
本文介绍了带有两个分隔符的正则表达式拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JavaScript,有没有一种方法可以将字符串拆分为具有两个分隔符的数组:':'和','

Using JavaScript, Is there a way to split the string to an array with two separators: ':' and ','

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

For var str = "21:223, 310:320";

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

would like the Result to be: [21, 223, 310, 320];

谢谢!

推荐答案

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

You could use a regular expression which looks for a : or for a comma with an optional space ,.

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

这篇关于带有两个分隔符的正则表达式拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆