简体   繁体   English

如何从 JavaScript 中的字符串中提取基本 URL?

[英]How to extract base URL from a string in JavaScript?

I'm trying to find a relatively easy and reliable method to extract the base URL from a string variable using JavaScript (or jQuery).我试图找到一种相对简单可靠的方法来使用 JavaScript(或 jQuery)从字符串变量中提取基本 URL。

For example, given something like:例如,给定如下内容:

http://www.sitename.com/article/2009/09/14/this-is-an-article/

I'd like to get:我想得到:

http://www.sitename.com/

Is a regular expression the best bet?正则表达式是最好的选择吗? If so, what statement could I use to assign the base URL extracted from a given string to a new variable?如果是这样,我可以使用什么语句将从给定字符串中提取的基本 URL 分配给新变量?

I've done some searching on this, but everything I find in the JavaScript world seems to revolve around gathering this information from the actual document URL using location.host or similar.我已经对此进行了一些搜索,但是我在 JavaScript 世界中找到的所有内容似乎都围绕使用location.host或类似方法从实际文档 URL 收集此信息。

Edit: Some complain that it doesn't take into account protocol.编辑:有些人抱怨它没有考虑协议。 So I decided to upgrade the code, since it is marked as answer.所以我决定升级代码,因为它被标记为答案。 For those who like one-line-code... well sorry this why we use code minimizers, code should be human readable and this way is better... in my opinion.对于那些喜欢单行代码的人……很抱歉,这就是我们使用代码最小化器的原因,代码应该是人类可读的,而且这种方式更好……在我看来。

var pathArray = "https://somedomain.com".split( '/' );
var protocol = pathArray[0];
var host = pathArray[2];
var url = protocol + '//' + host;

Or use Davids solution from below.或者使用下面的Davids 解决方案

WebKit-based browsers, Firefox as of version 21 and current versions of Internet Explorer (IE 10 and 11) implement location.origin .基于 WebKit 的浏览器、Firefox 21 版和当前版本的 Internet Explorer(IE 10 和 11)都实现了location.origin

location.origin includes the protocol , the domain and optionally the port of the URL. location.origin包括协议和可选的 URL端口

For example, location.origin of the URL http://www.sitename.com/article/2009/09/14/this-is-an-article/ is http://www.sitename.com .例如,URL http://www.sitename.com/article/2009/09/14/this-is-an-article/ location.originhttp://www.sitename.com

To target browsers without support for location.origin use the following concise polyfill:要定位不支持location.origin浏览器,请使用以下简洁的 polyfill:

if (typeof location.origin === 'undefined')
    location.origin = location.protocol + '//' + location.host;

不需要使用jQuery,只需使用

location.hostname

There is no reason to do splits to get the path, hostname, etc from a string that is a link.没有理由进行拆分以从作为链接的字符串中获取路径、主机名等。 You just need to use a link你只需要使用一个链接

//create a new element link with your link
var a = document.createElement("a");
a.href="http://www.sitename.com/article/2009/09/14/this-is-an-article/";

//hide it from view when it is added
a.style.display="none";

//add it
document.body.appendChild(a);

//read the links "features"
alert(a.protocol);
alert(a.hostname)
alert(a.pathname)
alert(a.port);
alert(a.hash);

//remove it
document.body.removeChild(a);

You can easily do it with jQuery appending the element and reading its attr.您可以使用 jQuery 附加元素并读取其属性轻松完成此操作。

Update: There is now new URL() which simplifies it更新:现在有new URL()来简化它

 const myUrl = new URL("https://www.example.com:3000/article/2009/09/14/this-is-an-article/#m123") const parts = ['protocol', 'hostname', 'pathname', 'port', 'hash']; parts.forEach(key => console.log(key, myUrl[key]))

var host = location.protocol + '//' + location.host + '/';

Well, URL API object avoids splitting and constructing the url's manually.好吧, URL API 对象避免了手动拆分和构建 url。

 let url = new URL('https://stackoverflow.com/questions/1420881');
 alert(url.origin);
String.prototype.url = function() {
  const a = $('<a />').attr('href', this)[0];
  // or if you are not using jQuery 👇🏻
  // const a = document.createElement('a'); a.setAttribute('href', this);
  let origin = a.protocol + '//' + a.hostname;
  if (a.port.length > 0) {
    origin = `${origin}:${a.port}`;
  }
  const {host, hostname, pathname, port, protocol, search, hash} = a;
  return {origin, host, hostname, pathname, port, protocol, search, hash};

}

Then :然后 :

'http://mysite:5050/pke45#23'.url()
 //OUTPUT : {host: "mysite:5050", hostname: "mysite", pathname: "/pke45", port: "5050", protocol: "http:",hash:"#23",origin:"http://mysite:5050"}

For your request, you need :对于您的要求,您需要:

 'http://mysite:5050/pke45#23'.url().origin

Review 07-2017 : It can be also more elegant & has more features Review 07-2017 : 它也可以更优雅,更多功能

const parseUrl = (string, prop) =>  {
  const a = document.createElement('a'); 
  a.setAttribute('href', string);
  const {host, hostname, pathname, port, protocol, search, hash} = a;
  const origin = `${protocol}//${hostname}${port.length ? `:${port}`:''}`;
  return prop ? eval(prop) : {origin, host, hostname, pathname, port, protocol, search, hash}
}

Then然后

parseUrl('http://mysite:5050/pke45#23')
// {origin: "http://mysite:5050", host: "mysite:5050", hostname: "mysite", pathname: "/pke45", port: "5050"…}


parseUrl('http://mysite:5050/pke45#23', 'origin')
// "http://mysite:5050"

Cool!凉爽的!

If you're using jQuery, this is a kinda cool way to manipulate elements in javascript without adding them to the DOM:如果您使用 jQuery,这是一种在 javascript 中操作元素而不将它们添加到 DOM 的很酷的方法:

var myAnchor = $("<a />");

//set href    
myAnchor.attr('href', 'http://example.com/path/to/myfile')

//your link's features
var hostname = myAnchor.attr('hostname'); // http://example.com
var pathname = myAnchor.attr('pathname'); // /path/to/my/file
//...etc

A lightway but complete approach to getting basic values from a string representation of an URL is Douglas Crockford's regexp rule:从 URL 的字符串表示中获取基本值的一种轻松但完整的方法是 Douglas Crockford 的正则表达式规则:

var yourUrl = "http://www.sitename.com/article/2009/09/14/this-is-an-article/";
var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
var parts = parse_url.exec( yourUrl );
var result = parts[1]+':'+parts[2]+parts[3]+'/' ;

If you are looking for a more powerful URL manipulation toolkit try URI.js It supports getters, setter, url normalization etc. all with a nice chainable api.如果您正在寻找更强大的 URL 操作工具包,请尝试URI.js它支持 getter、setter、url 规范化等,所有这些都带有一个很好的可链接的 api。

If you are looking for a jQuery Plugin, then jquery.url.js should help you如果您正在寻找 jQuery 插件,那么jquery.url.js应该可以帮助您

A simpler way to do it is by using an anchor element, as @epascarello suggested.正如@epascarello 所建议的那样,一种更简单的方法是使用锚元素。 This has the disadvantage that you have to create a DOM Element.这样做的缺点是您必须创建一个 DOM 元素。 However this can be cached in a closure and reused for multiple urls:然而,这可以缓存在一个闭包中并重用于多个 url:

var parseUrl = (function () {
  var a = document.createElement('a');
  return function (url) {
    a.href = url;
    return {
      host: a.host,
      hostname: a.hostname,
      pathname: a.pathname,
      port: a.port,
      protocol: a.protocol,
      search: a.search,
      hash: a.hash
    };
  }
})();

Use it like so:像这样使用它:

paserUrl('http://google.com');

If you are extracting information from window.location.href (the address bar), then use this code to get http://www.sitename.com/ :如果您从 window.location.href(地址栏)中提取信息,则使用此代码获取http://www.sitename.com/

var loc = location;
var url = loc.protocol + "//" + loc.host + "/";

If you have a string, str , that is an arbitrary URL (not window.location.href), then use regular expressions:如果您有一个字符串str ,它是一个任意 URL(不是 window.location.href),则使用正则表达式:

var url = str.match(/^(([a-z]+:)?(\/\/)?[^\/]+\/).*$/)[1];

I, like everyone in the Universe, hate reading regular expressions, so I'll break it down in English:我和宇宙中的每个人一样,讨厌阅读正则表达式,所以我将用英语对其进行分解:

  • Find zero or more alpha characters followed by a colon (the protocol, which can be omitted)查找零个或多个字母字符后跟一个冒号(协议,可以省略)
  • Followed by // (can also be omitted)后跟//(也可以省略)
  • Followed by any characters except / (the hostname and port)后跟除 / 之外的任何字符(主机名和端口)
  • Followed by /其次是 /
  • Followed by whatever (the path, less the beginning /).后跟任何内容(路径,减去开头 /)。

No need to create DOM elements or do anything crazy.无需创建 DOM 元素或做任何疯狂的事情。

You can use below codes for get different parameters of Current URL您可以使用以下代码获取当前 URL 的不同参数

alert("document.URL : "+document.URL);
alert("document.location.href : "+document.location.href);
alert("document.location.origin : "+document.location.origin);
alert("document.location.hostname : "+document.location.hostname);
alert("document.location.host : "+document.location.host);
alert("document.location.pathname : "+document.location.pathname);

I use a simple regex that extracts the host form the url:我使用一个简单的正则表达式从 url 中提取主机:

function get_host(url){
    return url.replace(/^((\w+:)?\/\/[^\/]+\/?).*$/,'$1');
}

and use it like this并像这样使用它

var url = 'http://www.sitename.com/article/2009/09/14/this-is-an-article/'
var host = get_host(url);

Note, if the url does not end with a / the host will not end in a / .请注意,如果url不以/结尾,则host不会以/结尾。

Here are some tests:以下是一些测试:

describe('get_host', function(){
    it('should return the host', function(){
        var url = 'http://www.sitename.com/article/2009/09/14/this-is-an-article/';
        assert.equal(get_host(url),'http://www.sitename.com/');
    });
    it('should not have a / if the url has no /', function(){
        var url = 'http://www.sitename.com';
        assert.equal(get_host(url),'http://www.sitename.com');
    });
    it('should deal with https', function(){
        var url = 'https://www.sitename.com/article/2009/09/14/this-is-an-article/';
        assert.equal(get_host(url),'https://www.sitename.com/');
    });
    it('should deal with no protocol urls', function(){
        var url = '//www.sitename.com/article/2009/09/14/this-is-an-article/';
        assert.equal(get_host(url),'//www.sitename.com/');
    });
    it('should deal with ports', function(){
        var url = 'http://www.sitename.com:8080/article/2009/09/14/this-is-an-article/';
        assert.equal(get_host(url),'http://www.sitename.com:8080/');
    });
    it('should deal with localhost', function(){
        var url = 'http://localhost/article/2009/09/14/this-is-an-article/';
        assert.equal(get_host(url),'http://localhost/');
    });
    it('should deal with numeric ip', function(){
        var url = 'http://192.168.18.1/article/2009/09/14/this-is-an-article/';
        assert.equal(get_host(url),'http://192.168.18.1/');
    });
});

A good way is to use JavaScript native api URL object.一个好方法是使用 JavaScript 原生 api URL对象。 This provides many usefull url parts.这提供了许多有用的 url 部分。

For example:例如:

const url = 'https://stackoverflow.com/questions/1420881/how-to-extract-base-url-from-a-string-in-javascript'

const urlObject = new URL(url);

console.log(urlObject);


// RESULT: 
//________________________________
hash: "",
host: "stackoverflow.com",
hostname: "stackoverflow.com",
href: "https://stackoverflow.com/questions/1420881/how-to-extract-base-url-from-a-string-in-javascript",
origin: "https://stackoverflow.com",
password: "",
pathname: "/questions/1420881/how-to-extract-base-url-from-a-string-in-javaript",
port: "",
protocol: "https:",
search: "",
searchParams: [object URLSearchParams]
... + some other methods

As you can see here you can just access whatever you need.正如你在这里看到的,你可以访问任何你需要的东西。

For example: console.log(urlObject.host); // "stackoverflow.com"例如: console.log(urlObject.host); // "stackoverflow.com" console.log(urlObject.host); // "stackoverflow.com"

doc for URL网址的文档

function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));


    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}

You then can use it like this...然后你可以像这样使用它......

var str = 'http://en.wikipedia.org/wiki/Knopf?q=1&t=2';
var url = str.toUrl();

The value of url will be... url 的值将是...

{
"original":"http://en.wikipedia.org/wiki/Knopf?q=1&t=2",<br/>"protocol":"http:",
"domain":"wikipedia.org",<br/>"host":"en.wikipedia.org",<br/>"relativePath":"wiki"
}

The "var url" also contains two methods. “var url”还包含两个方法。

var paramQ = url.getParameter('q');

In this case the value of paramQ will be 1.在这种情况下,paramQ 的值为 1。

var allParameters = url.getParameters();

The value of allParameters will be the parameter names only. allParameters 的值将仅为参数名称。

["q","t"]

Tested on IE,chrome and firefox.在 IE、chrome 和 firefox 上测试。

Instead of having to account for window.location.protocol and window.location.origin, and possibly missing a specified port number, etc., just grab everything up to the 3rd "/":不必考虑 window.location.protocol 和 window.location.origin,也不必考虑指定的端口号等,只需抓取第 3 个“/”之前的所有内容:

// get nth occurrence of a character c in the calling string
String.prototype.nthIndex = function (n, c) {
    var index = -1;
    while (n-- > 0) {
        index++;
        if (this.substring(index) == "") return -1; // don't run off the end
        index += this.substring(index).indexOf(c);
    }
    return index;
}

// get the base URL of the current page by taking everything up to the third "/" in the URL
function getBaseURL() {
    return document.URL.substring(0, document.URL.nthIndex(3,"/") + 1);
}

这有效:

location.href.split(location.pathname)[0];

You can do it using a regex :您可以使用正则表达式来做到这一点:

/(http:\/\/)?(www)[^\/]+\//i

does it fit ?合身吗 ?

To get the origin of any url, including paths within a website ( /my/path ) or schemaless ( //example.com/my/path ), or full ( http://example.com/my/path ) I put together a quick function.要获取任何 url 的来源,包括网站内的路径 ( /my/path ) 或无模式 ( //example.com/my/path ) 或完整 ( http://example.com/my/path ) 我把一起快速功能。

In the snippet below, all three calls should log https://stacksnippets.net .在下面的代码段中,所有三个调用都应记录https://stacksnippets.net

 function getOrigin(url) { if(/^\\/\\//.test(url)) { // no scheme, use current scheme, extract domain url = window.location.protocol + url; } else if(/^\\//.test(url)) { // just path, use whole origin url = window.location.origin + url; } return url.match(/^([^/]+\\/\\/[^/]+)/)[0]; } console.log(getOrigin('https://stacksnippets.net/my/path')); console.log(getOrigin('//stacksnippets.net/my/path')); console.log(getOrigin('/my/path'));

This, works for me:这对我有用:

 var getBaseUrl = function (url) { if (url) { var parts = url.split('://'); if (parts.length > 1) { return parts[0] + '://' + parts[1].split('/')[0] + '/'; } else { return parts[0].split('/')[0] + '/'; } } };

var tilllastbackslashregex = new RegExp(/^.*\//);
baseUrl = tilllastbackslashregex.exec(window.location.href);

window.location.href gives the current url address from browser address bar window.location.href 从浏览器地址栏给出当前 url 地址

it can be any thing like https://stackoverflow.com/abc/xyz or https://www.google.com/search?q=abc tilllastbackslashregex.exec() run regex and retun the matched string till last backslash ie https://stackoverflow.com/abc/ or https://www.google.com/ respectively它可以是任何类似https://stackoverflow.com/abc/xyzhttps://www.google.com/search?q=abc untillastbackslashregex.exec() 运行正则表达式并重新调整匹配的字符串直到最后一个反斜杠即https ://stackoverflow.com/abc/https://www.google.com/分别

Implementation:执行:

const getOriginByUrl = url => url.split('/').slice(0, 3).join('/');

Test:测试:

getOriginByUrl('http://www.sitename.com:3030/article/2009/09/14/this-is-an-article?lala=kuku');

Result:结果:

'http://www.sitename.com:3030'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM