Search code examples
javascriptregexsplitmatchseparator

regex split string with two separators


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!


Solution

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