簡體   English   中英

jQuery正則表達式修剪URL字符串的開頭和結尾

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

我有帶有href屬性的鏈接,如下所示:

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

我想編寫一個僅將s_014654設置為變量的jquery函數,稍后將用它來創建其他url結構。

有人可以幫我這個語法。

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

現場演示


更新:(基於評論)

<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();

這將返回數組中的所有值。

現場演示

像這樣:

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

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

感謝gdoron為我用自己的正則表達式更新的小提琴: http : //jsfiddle.net/Fjp8E/2/

模式([^/]+)\\.html$查找一個或多個非斜杠字符,並在字符串末尾加上“ .html”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM