Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

javascript regex split produces too many items

I'm trying to split a string using either commas or whitespace. A comma can optionally be preceded and/or followed by whitespace, and whitespace by itself also counts as a delimiter. The code looks like this:

var answers= s.split(/(s*,s*)|s+/);

If s contains the string 'a b,c', I get a list (array) containing five items instead of the expected three:

0:a, 1:undefined, 2:b, 3:,, 4:c

Any advice as to what I'm doing wrong will be appreciated.

Phillip

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

That's because split does also push capturing groups to the result array:

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.

The space between a and b was matched by the whitespace, so the capturing group was undefined. The comma between b and c was matched by the group, so it became the fourth item of your array.

To solve the issue, just remove the capturing group:

var answers = s.split(/s*,s*|s+/);

If you had a more complex expression where you needed grouping, you could make it non-capturing like this:

var answers = s.split(/(?:s*,s*)|s+/);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...