2

I'm fairly new to any kind of language but I need to modify a code at my work because the guy doing it previously left and no replacement.

I basically would like to put in a variable a specific part of a url.

The URLs look like this:

http://www.test.com/abc/hhhhhh/a458/example

I need to extract the a458 part and put it in a variable. This part is always at the same place but can be of variable length.

The URLs always have the same structure. I tried /hhhhhh\/{1}[a-z0-9]+\/{1}/g but it doesn't fully work. It keeps the hhh and the /.

4
  • Will links always be http://test.com/abc/hhhhhh/? Commented Aug 22, 2013 at 14:31
  • I manage to get that far more or less /hhhhhh\/{1}[a-z0-9]+\/{1}/g but doesn't seem to stripe out the hhh part and both / Commented Aug 22, 2013 at 14:32
  • the structure of the url will always be the same Commented Aug 22, 2013 at 14:32
  • Please show us the code with which you have applied that regex to the string. What do you mean by "stripe out" for example? Commented Aug 22, 2013 at 14:45

1 Answer 1

2

no need for regex, just split it

var link = "http://www.test.com/abc/hhhhhh/a458/example";
var linkParts = link.split("/");
//If the link is always in that format then a458 or whatever 
//would replace it will be in index 5
console.log(linkParts[5]);
Sign up to request clarification or add additional context in comments.

3 Comments

will it work if there are more stuf after the example ? like /example?abc=2&xyz=0&xxxx=2013-08-22&zzz=2013-08-24
also if it starts at abc can i then do linkparts[3] ?
@user2707774: Yes (unless you're getting example without the query string) and yes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.