[英]How to split a javascript string on spaces and non alphanumeric characters while keeping spaces
How can I split this javascript string: hello here's johhny!
我怎样才能拆分这个 javascript 字符串:
hello here's johhny!
into an array ['hello', '', 'here', ''', 's', '', 'johnny', '!']
?进入数组
['hello', '', 'here', ''', 's', '', 'johnny', '!']
?
I know you use .split(/(\s+)/);
我知道你使用
.split(/(\s+)/);
for splitting on spaces while preserving them and you use .split(/[^A-Za-z]/)
for splitting on non alphanumerics, but how can you combine these statements?用于在保留空格的同时拆分空格,并且使用
.split(/[^A-Za-z]/)
用于拆分非字母数字,但是如何组合这些语句?
split
includes matched groups in the output array so just wrap your second regex with ()
split
在 output 数组中包含匹配的组,所以只需用()
包裹你的第二个正则表达式
const str = "hello here's johhny." const array = str.split(/([^A-Za-z])/).filter(Boolean) console.log(array)
I added .filter(Boolean)
to get rid of the empty string at the end我添加了
.filter(Boolean)
来去掉最后的空字符串
To split a string into an array using both a regular expression for whitespace characters and a regular expression for non-alphanumeric characters, you can use the String.prototype.split
method and pass in a combined regular expression as the argument.要同时使用空白字符的正则表达式和非字母数字字符的正则表达式将字符串拆分为数组,您可以使用
String.prototype.split
方法并将组合的正则表达式作为参数传递。
Here's an example of how you can split the string "hello here's johhny!"
下面是一个如何拆分字符串
"hello here's johhny!"
into an array using a combined regular expression:使用组合的正则表达式进入数组:
const str = "hello here's johhny!";
const result = str.split(/[^A-Za-z]|(\s+)/);
console.log(result); // ['hello', '', 'here', ''', 's', '', 'johnny', '!']
The combined regular expression /[^A-Za-z]|(\s+)/
consists of two parts:组合正则表达式
/[^A-Za-z]|(\s+)/
由两部分组成:
[^A-Za-z]
: This regular expression matches any character that is not an alphabetic character (AZ or az). [^A-Za-z]
:此正则表达式匹配任何非字母字符(AZ 或 az)的字符。(\s+)
: This regular expression matches one or more consecutive whitespace characters (such as spaces, tabs, or newlines). (\s+)
:此正则表达式匹配一个或多个连续的空白字符(例如空格、制表符或换行符)。 The |
|
symbol is used to separate the two parts of the regular expression, and it indicates that either of the two parts can be matched.符号用于分隔正则表达式的两部分,表示可以匹配两部分中的任何一部分。 This means that the combined regular expression will match either a non-alphabetic character or one or more consecutive whitespace characters.
这意味着组合的正则表达式将匹配非字母字符或一个或多个连续的空白字符。
I hope this helps!我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.