0

I used String.split(a) method. It breaks the string into some parts when a ooccurs as substring. But what I want is I will give a list of delimitters and string will be broken into pieces when any one of those delimitters occur. How would I do this?

0

1 Answer 1

3

Use a regex in the split

'abcdef'.split(/[bdf]/) //[ 'a', 'c', 'e', '' ]

Or even

'abcdef'.split(/b|d|f/) //[ 'a', 'c', 'e', '' ]

Also splitting on string

'Hello There World'.split(/\s?There\s?|\s+/) //[ 'Hello', 'World' ]

\s? to grab any spaces that may be with the word

Sign up to request clarification or add additional context in comments.

5 Comments

What if I want to add a string and a character both as delimitter?
@taufique Add a string? Like a word? Then group the string in the regex. Basically anything that makes the regex match will be used to split. You should research regex
Would you please edit your answer for that case too?
@taufique—/bc|f/ splits on 'bc' and 'f'.
@taufique Splitting with both a "string" and a character: /puppy|kitten|[bdf]/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.