【问题标题】:remove hostname and port from url using regular expression使用正则表达式从 url 中删除主机名和端口
【发布时间】:2012-07-18 01:03:57
【问题描述】:

我正在尝试删除

http://localhost:7001/

部分

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

获取输出为

www.facebook.com

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

【问题讨论】:

  • 该网址是如何生成的?好像不太对……
  • 我不知道为什么我的问题被否决了,尽管我的问题还没有得到完美的答案。
  • @Uppi 可能是因为您正在寻求解决方案,而自己却没有表现出任何努力。
  • 我在网上搜索了很长时间,但我无法找到正确的答案。这就是我在这里发布的原因。
  • 也许找到第一次出现的句点,然后从那里获取字符串的其余部分以及第一个句点和前一个 / 之前的所有内容(如果没有,则获取字符串的开头。 ..)。您的网址会采用一致的格式吗?也就是说,它们都会以 http:// 开头吗?

标签: javascript html regex


【解决方案1】:

只需使用replace

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

【讨论】:

  • 它适用于我的本地机器,而不适用于其他环境,如 QA、URL 不同的生产环境。所以,我想要一个正则表达式模式。
  • 那么你如何决定在哪里截断url?
  • localhost:7001/www.facebook.com。我必须切断www.facebook.com前面的部分。我将根据localhost:7001中的最后一个/来切断
  • 你怎么知道最后一个/ 在哪里?它是整个字符串中的最后一个吗?意思是假设您没有像http://localhost/www.facebook.com/test 这样的网址是否安全?困难的部分不是编写正则表达式。如果你学习正则表达式,这很容易。困难的部分是知道你想要什么。
【解决方案2】:

到 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);

看看这段代码在行动Here

问候, 维克多

【讨论】:

  • 但如果有 https 则 var newURL = URL.replace (/^[az]{5}\:\/{2}[az]{1,}\:[0-9] {1,4}.(.*)/, '$1');
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

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

    var URL = "http://localhost:7001/www.facebook.com";
    
    var URLsplit = URL.split('/');
    
    var host = URLsplit[0] + "//" + URLsplit[2] + "/";
    
    var newURL = URL.replace(host, '');
    

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

    【讨论】:

    • 请注意,URL class 在 IE 中不起作用,截至 2017 年 6 月为“实验性”
    【解决方案5】:

    基于@atiruz 的回答,但这是

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

    【讨论】:

      【解决方案6】:

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

      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, '');
      

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

      demo on regex101

      【讨论】:

      • Bugs: 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-go-to=http://kittycats.com/pics'.replace(/^.*\/\/[^\/]+:?[0-9]?\//i, '')(这是一个好的 url,查询字符串可能包含 http://)
      【解决方案7】:

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

      var originSlash = /^https?:\/\/[^/]+\//i;
      
      theUrl.replace(originSlash, '');
      

      【讨论】:

        【解决方案8】:

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

        【讨论】:

          【解决方案9】:

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

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

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

          【讨论】:

          【解决方案10】:

          匹配 url 部分的正则表达式,您要删除,将类似于:/^http[s]?:\/\/.+?\//

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

          String urlWithBasePath = "http://localhost:7001/www.facebook.com";
          String resultUrl = urlWithBasePath.replaceFirst("^http[s]?:\\/\\/.+?\\/", ""); // resultUrl => www.facebook.com
          

          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# 代码示例(您还应指定using System.Text.RegularExpressions;):

          string urlWithBasePath = "http://localhost:7001/www.facebook.com";
          string resultUrl = Regex.Replace(urlWithBasePath, @"^http[s]?:\/\/.+?\/", ""); // resultUrl => www.facebook.com
          

          【讨论】:

            猜你喜欢
            • 2010-10-01
            • 1970-01-01
            • 2013-01-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-07-12
            • 2017-11-29
            • 1970-01-01
            相关资源
            最近更新 更多