简体   繁体   English

如何使用正则表达式在javascript中拆分字符串

[英]How to Split string In javascript Using regex

i am trying to do something like this 我正在尝试做这样的事情

function findLongestWord(str) {
var wordContainer = str.split(/\b/) || 0;
document.write(wordContainer);
}

findLongestWord("The quick brown fox jumped over the lazy dog");

but this returns 但这又回来了

The, ,quick, ,brown, ,fox, ,jumped, ,over, ,the, ,lazy, ,dog

however if i do something like this 但是如果我做这样的事情

function findLongestWord(str) {
var wordContainer = str.split(" ") || 0;
document.write(wordContainer);
}

findLongestWord("The quick brown fox jumped over the lazy dog");

it works as expected and returns 它按预期工作并返回

The,quick,brown,fox,jumped,over,the,lazy,dog

so why using /\\b/ is different from using " " in split ? 那么为什么在分割中使用/ \\ b /与使用“”不同?

\\b a Matches a zero-width word boundary, such as between a letter and a space. \\b a匹配零宽度的单词边界,例如字母和空格之间。

while using split(' ') only matches spaces: 在使用split(' ')仅匹配空格:

Docs on Regex from MDN 来自MDN的Regex文档

Because " " is a literal space, and \\b is a word boundary. 因为" "是文字空间, \\b是单词边界。

Word boundaries occur before the first character in a string, if the first character is a word character, and then again after the last character in the string, if the last character is a word character, and also between two characters in the string, where one is a word character and the other is not a word character, meaning your string looks like this with the boundaries : 如果第一个字符是单词字符,则单词边界出现在字符串的第一个字符之前;如果最后一个字符是单词字符,则在字符串的最后一个字符之后,再出现在字符串的两个字符之间,一个是单词字符,另一个不是单词字符,这意味着您的字符串看起来像这样,带有边界:

"The\b \bquick\b \bbrown\b \bfox\b \bjumped\b \bover\b \bthe\b \blazy\b \bdog"

In other words, you're matching the \\b at the beginning of the word, and at the end of the word, and you're getting the spaces as well, as you're splitting, and end up with 换句话说,您要在单词的开头和结尾匹配\\b ,并且在拆分时也得到空格,最后得到

["The"," ","quick"," ","brown"," ","fox"," ","jumped"," ","over"," ","the"," ","lazy"," ","dog"]

If you want to split words on boundaries, you have to add both of them, and anything in the middle, as in /\\b.\\b/ 如果要在边界上分割单词,则必须同时添加它们和中间的任何内容,例如/\\b.\\b/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM