Leon Revill
Leon Revill

Reputation: 2010

JavaScript Regex - match without the trailing slash

Given the following regular expression:

\/page1\/id\/(.*)\/type\/(.*)\/$

Which is to match the following URL path (and works):

/page1/id/331/type/test23/

How can I modify the regex to still match with the last slash missing, i.e:

/page1/id/331/type/test23

Thank you!

Upvotes: 0

Views: 779

Answers (1)

mpen
mpen

Reputation: 282885

Just throw a question mark before the final slash to make it optional.

\/page1\/id\/\d+\/type\/[^\/]+\/?$

Also, you may want to make that ID non-greedy, or match numbers specifically (updated regex for you).

Upvotes: 4

Related Questions