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

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -