0

I've looked on similiar topics but no one seems to answer my question.

I've URL that looks like this:

https://dummy.com/job/test

I need to extract test so I am using:

 function getIdentificator(){
 let URL = window.location.pathname;
 let Id = URL.slice(URL.lastIndexOf('/') + 1);
 return Id;
}

It gives me what I want but sometimes the URL is different. For example:

 https://dummy.com/job/testwz/something

I only need testwz.

Or:

 https://dummy.com/job/test-ab?somethingmore2132

I only need test-ab.

Or:

 https://dummy.com/job/test

I only need test.

Or:

 https://dummy.com/job/5423

I need 5423 from this.

Value I'm interested in always appear after job/ but in different variations as said before. Key value may be followed by: nothing, / or ?. Is there any way to extract this value in all examples with JavaScript? If not I can use jQuery as well.

Taplar
  • 24,788
  • 4
  • 22
  • 35
krascos
  • 3
  • 2
  • 2
    `urlVariable.split('/').pop()` This question has been asked before. – Taplar Feb 06 '19 at 15:22
  • @taplar answer is good. Or use a regular expression (regexp). Should be fairly trivial. – silencedogood Feb 06 '19 at 15:22
  • 1
    Possible duplicate of [How to get the value from the GET parameters?](https://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters) – Heretic Monkey Feb 06 '19 at 15:26
  • Or use the URL class, as shown in the duplicate... – Heretic Monkey Feb 06 '19 at 15:26
  • @HereticMonkey I think that would only work if the value being sought was actually a querystring parameter. In the examples given you can see it is mostly actually looking for the part of the URL which occurs after the "job" section. – ADyson Feb 06 '19 at 15:40
  • 1
    @ADyson Retracted the close vote. Now, my challenge to you: find the duplicate of this question, since I know there are many. – Heretic Monkey Feb 06 '19 at 15:40
  • krascos, will "job" always be the first part of the URL path following the domain? If so then [Mike's answer](https://stackoverflow.com/a/54557286/5947043) is the simple solution. If that is not a consistent thing, then you might need something more complex like a regular expression. – ADyson Feb 06 '19 at 15:55
  • @Taplar that won't work because in an example like `https://dummy.com/job/testwz/something` as given in the question, the target part of the URL (`testwz`) is not the last item. – ADyson Feb 06 '19 at 15:55
  • @ADyson Thanks for your effort. 'Job' will always be a part of the URL path. I'll test it in a few hours and let you guys now if it works. – krascos Feb 06 '19 at 16:56
  • " 'Job' will always be a part of the URL path." ...ok thanks but if you read my comment again you'll see that's not what I asked. I asked if it will always be **in the same place** in the URL path (i.e. in the first position after the domain name) - that assumption is crucial to the answer I linked to. – ADyson Feb 06 '19 at 16:58
  • @ADyson Sorry, I was in hurry. "Job" will always be in the same place (after domain name) so the Mike's answer is complex solution to my problem. Once again thank you very much. – krascos Feb 06 '19 at 19:06

2 Answers2

2

Assuming your path will always begin with /job no matter the domain:

return window.location.pathname.split('/')[2]
DevMike
  • 1,630
  • 2
  • 19
  • 33
-1

I'm going to give you this example: this is the question's url: https://stackoverflow.com/questions/54556911/how-to-extract-specific-parameter-from-different-urls

if you do window.location.pathname you will get : "/questions/54556911/how-to-extract-specific-parameter-from-different-urls"

now, if you do...

window.location.pathname.split('/').pop()

you will get:

how-to-extract-specific-parameter-from-different-urls

And I think this is the answer you are looking for.

Richard Fazzi
  • 433
  • 3
  • 9
  • In the examples given you can see it is actually looking for the part of the URL which occurs after the "job" section...but I don't know if it's guaranteed that it will always be at a specific index. Also your answer would be a lot more helpful if it actually dealt with the scenarios given in the question rather than an invented one. – ADyson Feb 06 '19 at 15:36
  • `window.location.pathname.split('/').pop()` will be too simplistic in a case given in the question: `https://dummy.com/job/testwz/something` because "testwz" is not the last item. Demo: https://jsfiddle.net/jx8w6rL0/3/ . – ADyson Feb 06 '19 at 15:49
  • 1
    Again I urge you to use the actual examples from the question - that way you can actually test if your code meets all the different requirements, as well as making the answer much more relevant. – ADyson Feb 06 '19 at 15:49