1

次のような href 属性のリンクがあります。

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

s_014654だけを変数に設定する jquery 関数を書きたいと思います。これは、後で別の URL 構造を作成するために使用します。

誰かがその構文で私を助けてくれますか?

4

2 に答える 2

3
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: </p>

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

これにより、配列内のすべての値が返されます。

ライブデモ

于 2012-06-06T22:30:23.213 に答える