views:

59

answers:

5

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?

+3  A: 

Try the following:

test.match(/\w+\s+/g); // \w = words, \s = white spaces
Motti
Or if the last bit of whitespace is optional: `test.match(/\w+\s*/gi)`
WoLpH
+1  A: 

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.

codaddict
A: 

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

Felix Kling
+1  A: 

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.
Kobi
A: 

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
palswim