简体   繁体   English

jQuery正则表达式修剪URL字符串的开头和结尾

[英]jquery regex trim beginning and end of a url string

I have links with href attributes that look like this: 我有带有href属性的链接,如下所示:

http://company.com/inetucm/groups/public/@enterprise/documents/pagecontent/s_014654.html 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. 我想编写一个仅将s_014654设置为变量的jquery函数,稍后将用它来创建其他url结构。

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: 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/ 感谢gdoron为我用自己的正则表达式更新的小提琴: 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. 模式([^/]+)\\.html$查找一个或多个非斜杠字符,并在字符串末尾加上“ .html”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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