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?
Asked
Active
Viewed 134 times
0
1 Answers
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

Gabs00
- 1,869
- 1
- 13
- 12
-
What if I want to add a string and a character both as delimitter? – taufique Aug 21 '14 at 03:19
-
@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 – Gabs00 Aug 21 '14 at 03:21
-
Would you please edit your answer for that case too? – taufique Aug 21 '14 at 03:24
-
1@taufique—`/bc|f/` splits on 'bc' and 'f'. – RobG Aug 21 '14 at 03:30
-
1@taufique Splitting with both a "string" and a character: `/puppy|kitten|[bdf]/` – Aug 21 '14 at 03:46