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

Javascript 使用正则表达式从url中删除主机名和端口,javascript,html,regex,Javascript,Html,Regex,我正试着移除 http://localhost:7001/ 离开 http://localhost:7001/www.facebook.com 将输出作为 www.facebook.com 我可以使用什么正则表达式来实现这个精确模式?只需使用 对于javascript,您可以使用以下代码: var URL = "http://localhost:7001/www.facebook.com"; var newURL = URL.replace (/^[a-z]{4,5}\:\/{2}[a-z

我正试着移除

http://localhost:7001/
离开

http://localhost:7001/www.facebook.com
将输出作为

www.facebook.com
我可以使用什么正则表达式来实现这个精确模式?

只需使用


对于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}.(.*)/, '$1'); // http or https
alert (newURL);
看看这段代码在运行

问候,,
Victor

或者,您可以使用的解析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

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

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

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

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

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

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

基于@atiruz answer,但这是

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

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

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


请参见

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


您可以使用浏览器解析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'

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

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

要匹配url中要删除的部分的正则表达式如下:
/^http[s]?:\/\/.+?\/

Java代码示例(请注意,在Java中,我们使用两个反斜杠“\\”来转义字符):

JS代码示例:

let urlWithBasePath = "http://localhost:7001/www.facebook.com";
let resultUrl = urlWithBasePath.replace(/^http[s]?:\/\/.+?\//, ''); // resultUrl => www.facebook.com
Python代码示例:

import re
urlWithBasePath = "http://localhost:7001/www.facebook.com"
resultUrl = re.sub(r'^http[s]?:\/\/.+?\/', '', urlWithBasePath) # resultUrl => www.facebook.com
示例或Ruby代码:

urlWithBasePath = "http://localhost:7001/www.facebook.com"
resultUrl =  urlWithBasePath = urlWithBasePath.sub(/^http[s]?:\/\/.+?\//, '') # resultUrl => www.facebook.com
PHP代码示例:

$urlWithBasePath = "http://localhost:7001/www.facebook.com";
$resultUrl = preg_replace('/^http[s]?:\/\/.+?\//', '', $urlWithBasePath); // resultUrl => www.facebook.com
C代码示例(还应使用System.Text.RegularExpressions;)指定


这个url是如何生成的?这似乎不对……我不知道为什么我的问题被否决了,尽管我的问题还没有得到一个完美的答案。@Uppi可能是因为你在寻求一个解决方案,而自己却毫不费力。我在网上搜索了这么长时间,但是我找不到正确的答案。这就是我在这里发布的原因。可能找到一个句点的第一次出现,然后从那里抓取字符串的其余部分,以及第一个句点和前一个句点之前的所有内容/(或者字符串的开头,如果没有的话…)。您的URL格式是否一致?也就是说,它们都是以http://开头的吗?它在我的本地机器上工作,而不是在其他环境(如QA、生产环境)中,在这些环境中url会有所不同。因此,我需要一个正则表达式模式。那么,您如何决定在何处切断url?。我必须剪掉www.facebook.com前面的部分。我将根据中的last/剪掉。你怎么知道最后的
/
在哪里?它是整个字符串中的最后一个吗?也就是说,假设您没有像
http://localhost/www.facebook.com/test
?困难的部分不是编写正则表达式。如果你学正则表达式,那很容易。困难的部分是知道你想要什么。请注意,在IE中不起作用,并且在2017年6月是“实验性”的,但是如果有https,那么var newURL=URL.replace(/^[a-z]{5}\:\/{2}[a-z]{1,}\:[0-9]{1,4}.(.*/,'$1');错误:1)尝试此操作,它将删除部分URL路径:
'http://example.com/double-slash-in-url-path//oops/the-path/got-broken“.replace(//^.*\/\/[^\/]+:?[0-9]?\//i')
Bug 2)
[0-9]?
只匹配一个数字,但端口号通常为4位,其他可能更常见1)示例:
'http://example.com/do-something?then-去=http://kittycats.com/pics“.replace(//^.*\/\/[^\/]+:?[0-9]?\//i,”)
(这是一个确定的url,查询字符串可能包含http://)抱歉,我尝试找到了以下地址的轮询:(.尝试使用此库@grinderzt此库不适用于React Native或nodeJs
let urlWithBasePath = "http://localhost:7001/www.facebook.com";
let resultUrl = urlWithBasePath.replace(/^http[s]?:\/\/.+?\//, ''); // resultUrl => www.facebook.com
import re
urlWithBasePath = "http://localhost:7001/www.facebook.com"
resultUrl = re.sub(r'^http[s]?:\/\/.+?\/', '', urlWithBasePath) # resultUrl => www.facebook.com
urlWithBasePath = "http://localhost:7001/www.facebook.com"
resultUrl =  urlWithBasePath = urlWithBasePath.sub(/^http[s]?:\/\/.+?\//, '') # resultUrl => www.facebook.com
$urlWithBasePath = "http://localhost:7001/www.facebook.com";
$resultUrl = preg_replace('/^http[s]?:\/\/.+?\//', '', $urlWithBasePath); // resultUrl => www.facebook.com
string urlWithBasePath = "http://localhost:7001/www.facebook.com";
string resultUrl = Regex.Replace(urlWithBasePath, @"^http[s]?:\/\/.+?\/", ""); // resultUrl => www.facebook.com