Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string with multiple separators

I have a string where I need to separate it by +-*/ and put it into array.

I've tried this code that I also found here, but it doesn't seem to be working. It gives me the error "Invalid regular expression: /+|-|*|//: Nothing to repeat."

var separators = ['+', '-', '*', '/'];
var numbers = x.split(new RegExp(separators.join('|'), ''));

Any advice on how should I do this?

like image 662
AyakoS Avatar asked Sep 19 '17 06:09

AyakoS


1 Answers

Try this.

var str = "i-have_six*apples+doyou/know.doe";
console.log(str.split(/[.\*+-/_]/));
like image 180
Harsha Avatar answered Sep 27 '22 20:09

Harsha