20

I want to split a string with all non-alphabetic characters as delimiters.

For example, I want to split this string

"hello1 twenty-three / nine.bye"

into

["hello","","twenty","three","","","nine","bye"]

I've tried this

text.split(/\[A-Za-z]+/)

but it isn't working.

How do I split a string by non-alphabetic characters?

Peter Olson
  • 139,199
  • 49
  • 202
  • 242
  • 1
    Since you're wanting to match sequential letters, why not use `.match()`? `text.match(/[A-Za-z]+/g)` –  Mar 23 '12 at 16:20

2 Answers2

37

It sounds like you're looking for the not a match atom: [^. Try the following

text.split(/[^A-Za-z]/)
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 3
    You can shorten ``/[^A-Za-z]/`` to ``/[^a-z]/i``. The ``i`` makes the regexp case-insensitive. – kmoser Feb 22 '20 at 00:27
4

Isn't the inital backslash breaking your []? What about text.split(/[^A-Za-z]+/)?

"asdsd22sdsdd".split(/[^A-Za-z]/)
["asdsd", "", "sdsdd"]
Jamund Ferguson
  • 16,721
  • 3
  • 42
  • 50