0

I have a string which looks like this:

/part/:aaaa/:bbbb/some/:cccc

I need to get: aaaa, bbbb, cccc from this string. How can I do it? For now I can get aaaa and bbbb because it between : and /. But how can I get cccc?

To get aaaa and bbbb i use str.match(/:(.*)\//).

Nastro
  • 1,719
  • 7
  • 21
  • 40

1 Answers1

1

We can try doing a regex iteration on the pattern :([^\/]+), which would match any colon term, capturing only the portion you want.

var re = /:([^\/]+)/g;
var input = '/part/:aaaa/:bbbb/some/:cccc';
var m;

do {
    m = re.exec(input);
    if (m) {
        console.log(m[1]);
    }
} while (m);
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360