问题
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