javascript 按非字母字符拆分字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9842506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:00:20  来源:igfitidea点击:

Split string by non-alphabetic characters

javascriptstring

提问by Peter Olson

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?

如何按非字母字符拆分字符串?

回答by JaredPar

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

听起来您正在寻找 not a match atom: [^。尝试以下

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

回答by Jamund Ferguson

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

最初的反斜杠不是打破了你的[]? 怎么样text.split(/[^A-Za-z]+/)

"asdsd22sdsdd".split(/[^A-Za-z]/)
["asdsd", "", "sdsdd"]