简体   繁体   English

如何在保留空格的同时在空格和非字母数字字符上拆分 javascript 字符串

[英]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+)/由两部分组成:

  1. [^A-Za-z] : This regular expression matches any character that is not an alphabetic character (AZ or az). [^A-Za-z] :此正则表达式匹配任何非字母字符(AZ 或 az)的字符。
  2. (\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.

相关问题 如何将字符串拆分为字符,但在JavaScript中保留空格 - How to split string into characters but to keep spaces in javascript 如何从文本区域中删除所有非字母数字字符并返回由单个空格分隔的单词字符串 - How to strip all non alphanumeric characters from a textarea and return a string of words separated by single spaces 正则表达式检查字符串是否仅包含字母数字字符和空格 - javascript - Regex to check if string contains Alphanumeric Characters and Spaces only - javascript 使用javascript删除字符串中的所有非字母数字和任何空格 - Remove all non alphanumeric and any white spaces in string using javascript 使用javascript删除除字母数字和空格之外的所有字符 - Remove all characters except alphanumeric and spaces with javascript 如何在Javascript和HTML中的空格上分割字符串? - How to split a string on the spaces in Javascript & HTML? 如何在javascript中的行空格/中断处拆分字符串 - How to split a string on line spaces/breaks in javascript 使用非字母数字字符时,如何使填充空格结果具有相似的长度? - How to make padding spaces results of a similar length when using non alphanumeric characters? 根据字符数和空格数使用正则表达式拆分字符串 - Split a string with regex based on number of characters and spaces javascript正则表达式用空格验证字母数字文本并拒绝特殊字符 - javascript regex to validate alphanumeric text with spaces and reject special characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM