In general, commas, hyphens, and underscores are used as separators. So, if a string has multiple separators and you want to split that string on those separators, then pass a regular expression, i.e.,
[,\-_]+ to the
let str = "Doughnut-Pie_Cupcake,Pastry"; let arr = str.split(/[,\-_]+/); console.log(arr); //["Doughnut", "Pie", "Cupcake", "Pastry"]
Here is the explanation of the code:
1. The string in this example has multiple separators.
let str = "Doughnut-Pie_Cupcake,Pastry";
+ sign matches one or more than one occurrence of the item defined before it. In this example, the previous item is a character class, so the plus sign matches one or more occurrences of the comma, hyphen, and underscore.
let arr = str.split(/[,\-_]+/); console.log(arr); //["Doughnut", "Pie", "Cupcake", "Pastry"]