javascript 使用正则表达式从 url 中删除主机名和端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11550790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:38:30  来源:igfitidea点击:

remove hostname and port from url using regular expression

javascripthtmlregex

提问by mdp

I am trying to remove

我正在尝试删除

http://localhost:7001/

part from

部分来自

http://localhost:7001/www.facebook.com

to get the output as

得到输出为

www.facebook.com

what is the regular expression that i can use to achieve this exact pattern?

我可以用来实现这种精确模式的正则表达式是什么?

回答by atiruz

To javascript you can use this code:

对于javascript,您可以使用以下代码:

var URL = "http://localhost:7001/www.facebook.com";
var newURL = URL.replace (/^[a-z]{4,5}\:\/{2}[a-z]{1,}\:[0-9]{1,4}.(.*)/, ''); // http or https
alert (newURL);

Look at this code in action Here

看看这个代码在行动这里

Regards, Victor

问候, 维克多

回答by talklesscodemore

Based on @atiruz answer, but this is

基于@atiruz 的回答,但这是

url = url.replace( /^[a-zA-Z]{3,5}\:\/{2}[a-zA-Z0-9_.:-]+\//, '' );
  • shortest
  • can take https or ftp too
  • can take url with or without explicit port
  • 最短的
  • 也可以使用 https 或 ftp
  • 可以带或不带显式端口的 url

回答by Israel Perales

You don't need any library or REGEX

您不需要任何库或正则表达式

var url = new URL('http://localhost:7001/www.facebook.com')
console.log(url.pathname)

https://developer.mozilla.org/en-US/docs/Web/API/URL

https://developer.mozilla.org/en-US/docs/Web/API/URL

回答by DeVilFisCh

This is how I made it work without resorting to regular expressions:

这就是我在不使用正则表达式的情况下使其工作的方式:

var URL = "http://localhost:7001/www.facebook.com";

var URLsplit = URL.split('/');

var host = URLsplit[0] + "//" + URLsplit[2] + "/";

var newURL = URL.replace(host, '');

Might not be an elegant solution though but it should be easier to understand for those who don't have much experience with regex (like me! ugh!).

虽然可能不是一个优雅的解决方案,但对于那些没有太多正则表达式经验的人来说应该更容易理解(像我一样!呃!)。

回答by Bradley Flood

For a simple regex to match any protocol, domain, and (optionally) port:

对于匹配任何协议、域和(可选)端口的简单正则表达式:

var url = 'http://localhost:7001/www.facebook.com';

// Create a regex to match protocol, domain, and host
var matchProtocolDomainHost = /^.*\/\/[^\/]+:?[0-9]?\//i;

// Replace protocol, domain and host from url, assign to `myNewUrl`
var myNewUrl = url.replace(matchProtocolDomainHost, '');

Now myNewUrl === 'www.facebook.com'.

现在myNewUrl === 'www.facebook.com'

See demo on regex101

参见regex101 上的演示

回答by KajMagnus

All other regular expressions here look a bit complicated? This is all that's needed: (right?)

这里的所有其他正则表达式看起来都有点复杂吗?这就是所需要的:(对吗?)

var originSlash = /^https?:\/\/[^/]+\//i;

theUrl.replace(originSlash, '');

回答by klues

instead of using regex you could just use the browser's capabilities of parsing an URL:

您可以不使用正则表达式,而是使用浏览器解析 URL 的功能:

var parser = document.createElement('a');
parser.href = "http://localhost:7001/www.facebook.com";
var path = parser.pathname.substring(1); // --> results in 'www.facebook.com'

回答by Strille

Alternatively, you can parse the url using as3corelib's URI class. That way you don't have to do any string manipulations, which helps to avoid making unintentional assumptions. It requires a few more lines of code, but it's a more general solution that should work for a wide variety of cases:

或者,您可以使用as3corelibURI 类解析 url 。这样您就不必进行任何字符串操作,这有助于避免做出无意的假设。它需要更多的代码行,但它是一个更通用的解决方案,应该适用于各种情况:

var url : URI = new URI("http://localhost:7001/myPath?myQuery=value#myFragment");

// example of useful properties
trace(url.scheme); // prints: http
trace(url.authority); // prints the host: localhost
trace(url.port); // prints: 7001
trace(url.path); // prints: /myPath
trace(url.query); // prints: myQuery=test
trace(url.fragment); // prints: myFragment

// build a new relative url, make sure we keep the query and fragment
var relativeURL : URI = new URI();
relativeURL.path = url.path;
relativeURL.query = url.query;
relativeURL.fragment = url.fragment;

var relativeURLString : String = relativeURL.toString();

// remove first / if any
if (relativeURLString.charAt(0) == "/") {
    relativeURLString = relativeURLString.substring(1, relativeURLString.length);
}

trace(relativeURLString); // prints: myPath?myQuery=test#myFragment

回答by sachleen

Just use replace

只需使用替换

"http://localhost:7001/www.facebook.com".replace("http://localhost:7001/",'')