Aaron Yodaiken
Aaron Yodaiken

Reputation: 19551

variable number of matched groups in a regex?

So I'd like to parse out strings like this (don't worry—this is only Node.JS which seems to sort of support unicode in RegEx):

var s = 'hello :你好:大家好:您好:吃饭了吗:';
console.log(s.match(/^([^ ]+) :([^:]+:)*/));

and get back groups of

[
    hello,
    你好:,
    大家好:,
    您好:,
    吃饭了吗:
]

However, right now, only [hello, 吃饭了吗] is the result.

Do I really need to split the results of the regex to achieve what I want?

Upvotes: 1

Views: 614

Answers (1)

thejh
thejh

Reputation: 45578

Yes, you have to. Groups only remember the last value they captured.

Upvotes: 1

Related Questions