开发者

Splitting string into array of words using Regular Expressions

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 开发者_如何学Python= /[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?


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.


Try the following:

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


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.


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).


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜