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?

AstroCB
  • 12,101
  • 20
  • 56
  • 70
taufique
  • 2,587
  • 1
  • 24
  • 39

1 Answers1

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,829
  • 1
  • 13
  • 12