How do I split a string with multiple separators in JavaScript? I'm trying to split on both commas and spaces but, AFAIK, js's split function only supports one separator.
-
2What have you tried? Can you just replace all spaces with commas and then split on commasjzworkman– jzworkman2012年03月15日 16:26:54 +00:00Commented Mar 15, 2012 at 16:26
1 Answer 1
The split function takes a regular expression.
> "Hello, world. How are you?".split(/[ ,]/);
[ 'Hello',
'',
'world.',
'How',
'are',
'you?' ]
(You might want [ ,]+ instead)
answered Mar 15, 2012 at 16:26
Quentin
949k137 gold badges1.3k silver badges1.4k bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js