regex split string with two separators

坚强是说给别人听的谎言 提交于 2019-12-11 02:43:04

问题


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

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

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

Thanks!


回答1:


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(/:|, */));



回答2:


You can use match if your expression is like this "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"]


来源:https://stackoverflow.com/questions/39810097/regex-split-string-with-two-separators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!