jq怎么获取一个url中的某个html的名字

clipboard.png

就想要获取url中的这两个值

阅读 4.2k
5 个回答
var test = 'http://www.baidu.com/haha/index/member.html';

var d1 = test.substring(test.lastIndexOf('/')); 
var d2 = test.substr(test.lastIndexOf('/', test.lastIndexOf('/') - 1));

var d3 = test.match(/\/[^\/]+\w$/g)[0];
var d4 = test.match(/\/\w+\/[^\/]+\w$/g)[0];

console.log(d1)  // -> /member.html
console.log(d2)  // -> /index/member.html
console.log(d3)  // -> /member.html
console.log(d4)  // -> /index/member.html

window.location 对象中截取

window.location.pathname.match(/(\w+)\/(\w+.html)$/) 取数组中的结果,不要做伸手党。

用正则截取 window.location.href

给你个比较全面的吧

function parseURL(url) {
                var a =  document.createElement('a');
                a.href = url;
                return {
                    source: url,
                    protocol: a.protocol.replace(':',''),
                    host: a.hostname,
                    port: a.port,
                    query: a.search,
                    params: (function(){
                        var ret = {},
                            seg = a.search.replace(/^\?/,'').split('&'),
                            len = seg.length, i = 0, s;
                        for (;i<len;i++) {
                            if (!seg[i]) { continue; }
                            s = seg[i].split('=');
                            ret[s[0]] = s[1];
                        }
                        return ret;
                    })(),
                    file: (a.pathname.match(/([^/?#]+)$/i) || [,''])[1],
                    hash: a.hash.replace('#',''),
                    path: a.pathname.replace(/^([^/])/,'/$1'),
                    relative: (a.href.match(/tps?:\/[^/]+(.+)/) || [,''])[1],
                    segments: a.pathname.replace(/^\//,'').split('/')
                };
            }
            var myURL = parseURL('http://abc.com:8080/dir/index.html?id=255&m=hello#top');
  
                console.log(myURL.file);     // = 'index.html'
                console.log(myURL.hash);     // = 'top'
                console.log(myURL.host);     // = 'abc.com'
                console.log(myURL.query);    // = '?id=255&m=hello'
                console.log(myURL.params);   // = Object = { id: 255, m: hello }
                console.log(myURL.path);     // = '/dir/index.html'
                console.log(myURL.segments); // = Array = ['dir', 'index.html']
                console.log(myURL.port);     // = '8080'
                console.log(myURL.protocol); // = 'http'
                console.log(myURL.source);   // = 'http://abc.com:8080/dir/index.html?id=255&m=hello#top'
新手上路,请多包涵

来个简单的: location.pathname.split('/').reverse()[0]

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题