Chrillewoodz
Chrillewoodz

Reputation: 28368

Regexp - Match re-occurring pattern

I've got a recursive url which can have an infinite amount of ids after the first /pages bit:

/pages/987d6fg8976df898dsa/92834799437324834/923897439874202307/.....

And so on and so on..

As well as have /new or /edit at the end like:

/pages/987d6fg8976df898dsa/92834799437324834/923897439874202307/new
/pages/987d6fg8976df898dsa/92834799437324834/923897439874202307/edit

I need to match these recursive urls, so I came up with these regexp's:

/new:

^\/pages\/[a-zA-Z0-9-]{0,}/new$

/edit:

^\/pages\/[a-zA-Z0-9-]{0,}/edit$

The problem here is that it only matches strings that have one id string, so /pages/93848347s7a87sa9ds7d works but /pages/20839283792839289d/023898473947384349 fails.

I've tried looking for a regexp which solves this but I haven't come across any which suits my needs.

Any idea how I can solve this?

Upvotes: 0

Views: 50

Answers (2)

Toto
Toto

Reputation: 91518

I'd use:

/^\/pages\/(?:[a-zA-Z0-9]*\/)*(new|edit)$/

var test = [
    '/pages/987d6fg8976df898dsa/92834799437324834/923897439874202307/new',
    '/pages/987d6fg8976df898dsa/92834799437324834/923897439874202307/edit'
];
console.log(test.map(function (a) {
  return a+' :'+/^\/pages\/(?:[a-zA-Z0-9]*\/)*(new|edit)$/.test(a);
}));

Upvotes: 1

Panup Pong
Panup Pong

Reputation: 1891

Could you try this

^\/pages\/[a-zA-Z0-9-\/]{0,}\/new$

Upvotes: 1

Related Questions