The split()
function in JavaScript splits a string into an array by a separator and removes the separator:
// Split by "-"
"hello-from-javascript".split("-");
// Result: ["hello", "from", "javascript"]
// Split by "-" or "_"
"hello-from_javascript".split(/-|_/g);
// Result: ["hello", "from", "javascript"]
Use a RegEx group (...)
if you want to keep the separator in the array:
// Split by "-" or "_"
"hello-from_javascript".split(/(-|_)/g);
// Result: ["hello", "-", "from", "_", "javascript"]