[英]JavaScript split String with white space
I would like to split a String but I would like to keep white space like:我想拆分一个字符串,但我想保留空白,例如:
var str = "my car is red";
var stringArray [];
stringArray [0] = "my";
stringArray [1] = " ";
stringArray [2] = "car";
stringArray [3] = " ";
stringArray [4] = "is";
stringArray [5] = " ";
stringArray [6] = "red";
How I can proceed to do that?我如何才能继续这样做?
Thanks !谢谢 !
Using regex:使用正则表达式:
var str = "my car is red";
var stringArray = str.split(/(\s+)/);
console.log(stringArray); // ["my", " ", "car", " ", "is", " ", "red"]
\\s
matches any character that is a whitespace, adding the plus makes it greedy, matching a group starting with characters and ending with whitespace, and the next group starts when there is a character after the whitespace etc. \\s
匹配任何作为空格的字符,添加加号使其贪婪,匹配以字符开头并以空格结尾的组,当空格后面有一个字符时,下一组开始等。
You could split the string on the whitespace and then re-add it, since you know its in between every one of the entries.您可以在空白处拆分字符串,然后重新添加它,因为您知道它位于每个条目之间。
var string = "text to split";
string = string.split(" ");
var stringArray = new Array();
for(var i =0; i < string.length; i++){
stringArray.push(string[i]);
if(i != string.length-1){
stringArray.push(" ");
}
}
Update : Removed trailing space.更新:删除了尾随空格。
For split string by space like in Python lang, can be used:对于像 Python lang 那样按空格分割字符串,可以使用:
var w = "hello my brothers ;";
w.split(/(\s+)/).filter( function(e) { return e.trim().length > 0; } );
output:输出:
["hello", "my", "brothers", ";"]
or similar:或类似:
w.split(/(\s+)/).filter( e => e.trim().length > 0)
(output some) (输出一些)
You can just split on the word boundary using \\b
.您可以使用
\\b
在单词边界上进行拆分。 See MDN见MDN
"\\b: Matches a zero-width word boundary, such as between a letter and a space." “\\b:匹配零宽度的单词边界,例如字母和空格之间。”
You should also make sure it is followed by whitespace \\s
.您还应该确保其后跟空格
\\s
。 so that strings like "My car isn't red"
still work:所以像
"My car isn't red"
这样的字符串仍然有效:
var stringArray = str.split(/\b(\s)/);
The initial \\b
is required to take multiple spaces into account, eg my car is red
初始
\\b
需要考虑多个空格,例如my car is red
EDIT: Added grouping编辑:添加分组
Although this is not supported by all browsers, if you use capturing parentheses inside your regular expression then the captured input is spliced into the result.尽管并非所有浏览器都支持这种方式,但如果您在正则表达式中使用捕获括号,则捕获的输入将拼接到结果中。
If separator is a regular expression that contains capturing parentheses, then each time separator is matched, the results (including any undefined results) of the capturing parentheses are spliced into the output array.
如果separator是一个包含捕获括号的正则表达式,那么每次匹配separator时,将捕获括号的结果(包括任何未定义的结果)拼接到输出数组中。 [ reference )
[ 参考]
So:所以:
var stringArray = str.split(/(\s+)/);
^ ^
//
Output:输出:
["my", " ", "car", " ", "is", " ", "red"]
This collapses consecutive spaces in the original input, but otherwise I can't think of any pitfalls.这会折叠原始输入中的连续空格,但除此之外我想不出任何陷阱。
str.split(' ').join('§ §').split('§');
In case you're sure you have only one space between two words, you can use this one如果你确定两个单词之间只有一个空格,你可以使用这个
str.replace(/\\s+/g, ' ').split(' ')
so you replace one space by two, the split by space所以你用两个替换一个空格,按空格分割
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.