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?
Use the re. split() method to split a string on all special characters. The re. split() method takes a pattern and a string and splits the string on each occurrence of the pattern.
A common solution to remove all non-alphanumeric characters from a String is with regular expressions. The idea is to use the regular expression [^A-Za-z0-9] to retain only alphanumeric characters in the string. You can also use [^\w] regular expression, which is equivalent to [^a-zA-Z_0-9] .
It's as simple as: s. split(""); The delimiter is an empty string, hence it will break up between each single character.
It sounds like you're looking for the not a match atom: [^
. Try the following
text.split(/[^A-Za-z]/)
Isn't the inital backslash breaking your []
? What about text.split(/[^A-Za-z]+/)
?
"asdsd22sdsdd".split(/[^A-Za-z]/)
["asdsd", "", "sdsdd"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With