鍍金池/ 問答/HTML/ jq怎么獲取一個url中的某個html的名字

jq怎么獲取一個url中的某個html的名字

clipboard.png

就想要獲取url中的這兩個值

回答
編輯回答
蝶戀花

window.location 對象中截取

window.location.pathname.match(/(\w+)\/(\w+.html)$/) 取數(shù)組中的結(jié)果,不要做伸手黨。

2017年4月26日 14:53
編輯回答
貓小柒

來個簡單的: location.pathname.split('/').reverse()[0]

2018年3月17日 08:39
編輯回答
吃藕丑

用正則截取 window.location.href

2017年8月18日 10:18
編輯回答
敢試

給你個比較全面的吧

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'
2017年2月15日 03:22
編輯回答
浪婳
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
2018年1月31日 09:15