-1

I am asking you how to split a string using different separators and when the string is empty return just an empty space. I don't know to combine both.

All I have is:

function split(string) {
  var str = string.split(/[+-*]/);
  return str;
}

Example:

split("este-es+otro*ejemplo"); // => ["este", "es", "otro", "ejemplo"]
split(''); // => [""]

Thank you.

1 Answers1

1

Move the * at the first position inside square bracket ([]).

If any special character, such as backslash (*) is immediately after the left square bracket, it doesn't have its special meaning and is considered to be one of the characters to match literally.

Try /[*+-]/g

function split(string) {
  var str = string.split(/[*+-]/g);
  return str;
}

console.log(split("este-es+otro*ejemplo"));
Mamun
  • 66,969
  • 9
  • 47
  • 59