简体   繁体   中英

jquery regex trim beginning and end of a url string

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM