1

I have links with href attributes that look like this:

http://company.com/inetucm/groups/public/@enterprise/documents/pagecontent/s_014654.html

I want to write a jquery function that sets just the s_014654 to a variable, that I will use later to create a different url stucture.

Can someone please help me with that syntax.

3
  • What do you mean by "a jQuery function"? Are you talking about creating a plugin? If you just want a re-usable function simply take either of the answers so far and encapsulate them in a standard function... Commented Jun 6, 2012 at 22:41
  • 1
    We have a debate over here, can you please tell us which of the two ways is easier for you to understand? Commented Jun 6, 2012 at 22:44
  • Are you still having troubles? if not you should accept one of the answers. (Writing "Accept" seems to be broken though) Commented Jun 6, 2012 at 23:51

2 Answers 2

3
var url = ....;
var arr = url.split('/');
var value = arr.pop().match(/^(.+)\.html$/)[1];
alert(value); // "s_014654"

Live DEMO


Update: (based of the comment)

<a href="http://company.com/inetucm/groups/public/@enterprise/documents/pagecontent/s_014654.html" />
<a href="http://company.com/inetucm/groups/public/@enterprise/documents/pagecontent/AAAAAAAA.html" />
<a href="http://company.com/inetucm/groups/public/@enterprise/documents/pagecontent/BBB.html" />    
<a href="http://company.com/inetucm/groups/public/@enterprise/documents/pagecontent/fdskl489j.html" />

jQuery:

var arr = $('a').map(function(){ 
    return this.href.split('/').pop().match(/^(.+)\.html$/)[1]; 
}).get();

This will return all the values in an array.

Live DEMO

Sign up to request clarification or add additional context in comments.

14 Comments

You can also do arr.pop().match( :-P
@zerkms. Yes, it's easier to read and understand, but it can be done with regex as well...
@gdoron: are you sure that replacing .+ with [^/]+ changes much --- stackoverflow.com/a/10923281/251311 ? Meanwhile - we need to think of split + pop
@zerkms. I asked the OP; It's interesting question, even more then the OP question... :)
@zerkms. You ought to read this. Yes it's off-topic, but damn it's funny!
|
2

Something like this:

var url = "http://company.com/inetucm/groups/public/@enterprise/documents/pagecontent/s_014654.html";

var value = url.match(/([^/]+)\.html$/)[1];

With thanks to gdoron for the fiddle that I updated with my own regex: http://jsfiddle.net/Fjp8E/2/

The pattern ([^/]+)\.html$ looks for one or more non-slash characters that are followed by ".html" at the end of the string.

2 Comments

Just worth saying, the regex grabs every char that doesn't have a slash and ends with .html. +1.
You ought to read this, that guy is so funny! I'm sorry, but I had to share it with someone...

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.