Splitting string into array of words using Regular Expressions

感情迁移 提交于 2021-01-27 04:11:07

问题


I'm trying to split a string into an array of words, however I want to keep the spaces after each word. Here's what I'm trying:

var re = /[a-z]+[$\s+]/gi;
var test = "test   one two     three   four ";
var results = test.match(re);

The results I expect are:

[0]: "test   "
[1]: "one "
[2]: "two     "
[3]: "three   "
[4]: "four "

However, it only matches up to one space after each word:

[0]: "test "
[1]: "one "
[2]: "two "
[3]: "three "
[4]: "four "

What am I doing wrong?


回答1:


Consider:

var results = test.match(/\S+\s*/g);

That would guarantee you don't miss any characters (besides a few spaces at the beginnings, but \S*\s* can take care of that)

Your original regex reads:

  • [a-z]+ - match any number of letters (at least one)
  • [$\s+] - much a single character - $, + or whitespace. With no quantifier after this group, you only match a single space.



回答2:


Try the following:

test.match(/\w+\s+/g); // \w = words, \s = white spaces



回答3:


You are using + inside the char class. Try using * outside the char class instead.

/[a-z]+\s*/gi;

+ inside the char class is treated as a literal + and not as a meta char. Using * will capture zero or more spaces that might follow any word.




回答4:


The + is taken literally inside the character class. You have to move it outside: [\s]+ or just \s+ ($ has no meaning inside the class either).




回答5:


The essential bit of your RegEx that needs changing is the part matching the whitespace or end-of-line.

Try:

var re = /[a-z]+($|\s+)/gi

or, for non-capturing groups (I don't know if you need this with the /g flag):

var re = /[a-z]+(?:$|\s+)/gi


来源:https://stackoverflow.com/questions/3548527/splitting-string-into-array-of-words-using-regular-expressions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!