删除 http:// 或 http:// 和 www

问题描述 投票:0回答:5

我正在尝试构建一个网址清理器。

我希望获取网址列表,并删除开头的所有

https://
http://
www.
等以及尾随
/
之后的所有文本。

我尝试了以下正则表达式

url.replace(/^https?\:\/\/www\./i, "").split('/')[0];

这在一定程度上有效并输出以下内容

"www.net-temps.com"
"www.toplanguagejobs.com"
"http:"
"peopleready.com"
"nationjob.com"
"http:"
"bluesteps.com"
"https:"
"theguardian.com"
"reddit.com"
"youtube.com"
"https:"
"pgatour.com"
"cultofmac.com"

来自以下列表:

'www.net-temps.com',
'www.toplanguagejobs.com',
'http://nychires.com/',
'http://www.peopleready.com/',
'https://www.nationjob.com/',
'http://nationaljobsonline.com/',
'https://www.bluesteps.com/',
'https://medium.freecodecamp.com/how-we-got-our-2-year-old-open-source-project-to-trend-on-github-8c25b0a6dfe9#.nl4985bjz',
'https://www.theguardian.com/uk/business',
'https://www.reddit.com/r/funny/comments/5qzkz4/my_captain_friend_sent_me_this_photo_saudi_prince/',
'https://www.youtube.com/watch?v=Bua8k_CcnuI',
'https://stackoverflow.com/questions/7000995/jquery-removing-part-of-string-after-and-removing-too/7001040#7001040',
'http://www.pgatour.com/fantasy.html',
'http://www.cultofmac.com/464645/apple-spaceship-campus-flyover/'

如果我从正则表达式中删除

/www\.
,效果很好,并删除所有
https:
等,但我也想删除
www.
(如果它存在),无论
https:

这是我到目前为止编码的内容

https://jsfiddle.net/xba5x9ro/1/

将来一旦解决了这个问题。我想从运行

makeDomainBeautiful
的文本区域获取网址列表并输出到另一个文本区域,但我想我应该先让它工作。

javascript regex
5个回答
49
投票

/^(?:https?:\/\/)?(?:www\.)?/i
,其中
https://
www.
都应该是可选的 (
?
) 和非捕获组 (
(?:...)
)。

var url = prompt("url: ");

url = url.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "").split('/')[0];

alert("url: " + url);


2
投票

这将处理

http
https
www

url.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "").split('/')[0]

0
投票

var url = prompt("url: ");

url = url.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "").split('/')[0];

alert("url: " + url);


0
投票

您可以在不使用 split 函数的情况下删除最后一个“/”和尾随字符串。 只需修改一下正则表达式即可。

const url = 'https://example.com/';

url = url.replace(/^(?:https?:\/\/)?(?:www\.)?|\/.*$/i, "");


-1
投票

根据 ibrahim mahrir 的回答,如果您只想从 URL 开头修剪 http 或 https 和 www,但保留其余部分。在 codepen 中模拟它以测试它是否有效。看起来效果很好。 https://codepen.io/pureth/pen/LQOaPz

var regex = /^(?:https?:\/\/)?(?:www\.)?/i;
var urlList = [
  "www.net-temps.com",
  "www.toplanguagejobs.com",
  "http://nychires.com/",
  "http://www.peopleready.com/",
  "https://www.nationjob.com/",
  "http://nationaljobsonline.com/",
  "https://www.bluesteps.com/",
  "https://medium.freecodecamp.com/how-we-got-our-2-year-old-open-source-project-to-trend-on-github-8c25b0a6dfe9#.nl4985bjz",
  "https://www.theguardian.com/uk/business",
  "https://www.reddit.com/r/funny/comments/5qzkz4/my_captain_friend_sent_me_this_photo_saudi_prince/",
  "https://www.youtube.com/watch?v=Bua8k_CcnuI",
  "https://stackoverflow.com/questions/7000995/jquery-removing-part-of-string-after-and-removing-too/7001040#7001040",
  "http://www.pgatour.com/fantasy.html",
  "http://www.cultofmac.com/464645/apple-spaceship-campus-flyover/"
];

urlList.forEach(function(url) {
  let $originalEl = $("<div class='url'>" + url + "</div>"),
    cleanUrl = url.replace(regex, ""),
    $cleanEl = $("<div class='url'>" + cleanUrl + "</div>");
  $(".original").append($originalEl);
  $(".clean").append($cleanEl);
});
.original, .clean {
  background-color: grey;
  width: 25%;
  max-width: 350px;
  float: left;
}
.title {
  color: white;
  text-align: center;
  padding-top: 3px;
}
.url {
  background-color: lightgrey;
  margin: 5px;
  word-wrap:break-word;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="original">
  <div class="title"><b>original</b></div>
</div>
<div class="clean">
  <div class="title"><b>clean</b></div>
</div>

© www.soinside.com 2019 - 2024. All rights reserved.