Split string with multiple separators

Viewed 16588

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?

3 Answers

use regex split

    var tempvar = (X).split(/[+-/*]+/);

This should return as array split. eg : X = 1+2-3/4

alert(x) would return as 

    1,2,3,4
Related