0

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.

asked Mar 15, 2012 at 16:24
1
  • 2
    What have you tried? Can you just replace all spaces with commas and then split on commas Commented Mar 15, 2012 at 16:26

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.