is it possible to make a regex with multiple delimiters? For example I want to split a string which can come in two forms: 1. “string1, string2, string3” or 2. “string1,string2,string3”. I’ve been trying to do this in javascript but with no success so far.
Share
Just use a regex
split():JS Fiddle demo.
The reason I’ve used
*rather than?is simply because it allows for no white-space or many white-spaces. Whereas the?matches zero-or-one white-space (which is exactly what you asked, but even so).Incidentally, if there might possibly be white-spaces preceding the comma, then it might be worth amending the
split()regex to:JS Fiddle demo.
Which splits the supplied string on zero-or-more whitespace followed by a comma followed by zero-or-more white-space. This may, of course, be entirely unnecessary.
References:
string.split().