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,337
  • 20
  • 57
  • 73
taufique
  • 2,701
  • 1
  • 26
  • 40

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