jQuery正则表达式修剪URL字符串的开头和结尾 [英] jquery regex trim beginning and end of a url string

查看:103
本文介绍了jQuery正则表达式修剪URL字符串的开头和结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有带有如下所示的href属性的链接:

I have links with href attributes that look like this:

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

我想编写一个仅将 s_014654 设置为变量的jquery函数,稍后将用它来创建不同的url结构.

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"

实时演示

更新:(基于评论)

<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.

实时演示

这篇关于jQuery正则表达式修剪URL字符串的开头和结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆