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

时间:2017-01-30 18:13:10

标签: javascript regex

嗨,大家好我正在尝试建立一个网址清洁工。

我希望获取一个网址列表,并从头开始删除所有https://http://www.等,以及跟踪{{1}后的所有文字}。

我尝试过以下正则表达式/

这在一定程度上起作用并输出以下内容

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://jsfiddle.net/xba5x9ro/1/

将来一旦分类。我想从运行https:的文本区域中获取一个网址列表,然后输出到另一个文本区域,但我认为我先让它工作。

3 个答案:

答案 0 :(得分:16)

TemplateParent其中setInitialViewBoxexport const setInitialViewBox = ( viewBox ) => { return({ type : SET_INITIAL_VIEWBOX, svgPayload : { defaultTab : { viewBox : viewBox, scale : 1 }, bodyTab : { viewBox : viewBox, scale : 1 }, sleeveTab : { viewBox : viewBox, scale : 1 } } }); }; 都应该是可选的(/^(?:https?:\/\/)?(?:www\.)?/i)和非捕获组(https://)。



www.




答案 1 :(得分:0)

根据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);
});
&#13;
.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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这将处理httphttpswww

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