0

Is there any effective/good way to match slug in URL ?

For instance :

match :

  • http://somepage.com/s/XXXXX/this-is-slug-i-want
  • http://somepage.com/s/XXXXX/this-is-slug-i-want/
  • http://somepage.com/s/XXXXX/this-is-slug-i-want?foo=bar
  • http://somepage.com/s/XXXXX/this-is-slug-i-want#foo

not to match :

  • http://somepage.com/s/XXXXX/somePage.html and similar (.php, etc.)
Maielo
  • 692
  • 1
  • 6
  • 20
  • 4
    Possible duplicate of [Javascript: match slug of a URL](http://stackoverflow.com/questions/17754146/javascript-match-slug-of-a-url) – Heretic Monkey Jun 03 '16 at 21:23
  • 1
    Please at least attempt a solution before asking on Stack Overflow. We're not a code-writing service. Also, as it says in [ask], please research before asking there have been a number of questions on this subject. – Heretic Monkey Jun 03 '16 at 21:24
  • I'd go with `.*this-is-slug-i-want.*` –  Jun 03 '16 at 21:29
  • This is pretty unclear. Do you want the string to match to the Regexp? do you want to compare the last slug? What are you trying to accomplish? – Jay Jun 03 '16 at 21:33
  • @sin that's incorrect, that regexp will match to things like `http://this-is-slug-i-want.com`, `http://somepage.coms/XXXXX/somePage.html?this-is-slug-i-want`, and a plethora of others that the OP definitely didn't want – Jay Jun 03 '16 at 21:59
  • @Wade - Well, I never heard the technical term `slug` before. Is that the definition you're inferring? –  Jun 03 '16 at 22:04
  • @sin yeah same, I'm pretty sure he means that it's in the filepath (this is probably not a term either) of the URL. That is to say, it's after the domain name such as `something.com/this-is-the-slug`, where `this-is-the-slug` is what the OP considers a "slug". Not sure if he wants it to be the last section of the URL though, or if he's okay with it matching to something like `something.com/this-is-slug-i-want/something-else` – Jay Jun 03 '16 at 22:47

2 Answers2

0

How about this...

http:\/\/somepage.com\/s\/XXXXX\/(?!.*\.html).*

Test: http://rubular.com/r/KyTWM1fizJ

notAduck
  • 190
  • 1
  • 3
  • 13
  • This would match anything with the form `http://somepage.com/s/XXXXX/literally-anything-without-.html-in-it`, not what the poster asked. – Jay Jun 03 '16 at 21:39
  • Two other options then... `http:\/\/somepage.com\/s\/XXXXX\/(?!.*\.(html|php)).*` or `http:\/\/somepage.com\/s\/XXXXX\/(?!.*\..*).*` – notAduck Jun 03 '16 at 21:43
  • Again, this is not what the poster asked. They want something where they have a slug they want to match but not other pages. They never said that slug can't have `.html` or `.php` or whatever other extension you can whip up in it. Yours just matches to any page without the specific suffix, not to a single desired slug as the poster asked. – Jay Jun 03 '16 at 21:54
  • Since it seems like you really want your solution to work, here's an example where yours fails. http://rubular.com/r/9bX47v2vJB – Jay Jun 03 '16 at 21:57
  • Clearly I am confused as to what OP wants. Seems to me OP is trying to look for URLs that end in a folder or in a querystring, but not in a filename - I think my regex accomplishes that, but maybe that's not what OP wants - I admit to being a bad psychic. – notAduck Jun 03 '16 at 22:02
  • My interpretation is that the OP wants to be able to specify which folder (not querystrying I'm pretty sure), but then again I guess he's not editing his question or responding to any of the comments so it's up in the air. I'm pretty sure he wants to be able to specify what folder or slug he wants at the end of his URL though – Jay Jun 03 '16 at 22:44
0

Why not /^http:\/\/.+\/this-is-the-slug-i-want\/?(\?.*)?/

Jay
  • 998
  • 1
  • 10
  • 22