Split a string based on multiple delimiters

This should work:

var separators = [' ', '+', '(', ')', '*', '\\/', ':', '?', '-'];
var tokens = x.split(new RegExp('[' + separators.join('') + ']', 'g'));​​​​​​​​​​​​​​​​​

Generated regex will be using regex character class: /[ +()*\/:?-]/g

This way you don't need to escape anything.


escape needed for regex related characters +,-,(,),*,?

var x = "adfds+fsdf-sdf";

var separators = [' ', '\\\+', '-', '\\\(', '\\\)', '\\*', '/', ':', '\\\?'];
console.log(separators.join('|'));
var tokens = x.split(new RegExp(separators.join('|'), 'g'));
console.log(tokens);

http://jsfiddle.net/cpdjZ/