javascript - Split a string based on multiple delimiters -


i trying split string based on multiple delimiters referring how split string in jquery multiple strings separator

since multiple delimiters decided follow

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

but i'm getting error

uncaught syntaxerror: invalid regular expression: / |+|-|(|)|*|/|:|?/: nothing repeat  

how solve it?

escape needed 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/


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -