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?
1 Answer
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
5 Comments
taufique
What if I want to add a string and a character both as delimitter?
Gabs00
@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
taufique
Would you please edit your answer for that case too?
RobG
@taufique—
/bc|f/ splits on 'bc' and 'f'.