【问题标题】:extract domain name from url javascript从 url javascript 中提取域名
【发布时间】:2021-04-12 20:01:20
【问题描述】:

我正在尝试从“推文”中的字符串中提取域名,如何避免从字符串中提取双反斜杠? 我在 let url 中定义的正则表达式

let tweets = [
  "Thank you to the Academy and the incredible cast & crew of #TheRevenant. #Oscars",
  "@HardingCompSci department needs student volunteers for #HourOfCode https://hourofcode.com/us",
  "Checkout the most comfortable earbud on #Kickstarter and boost your #productivity https://www.kickstarter.com/",
  "Curious to see how #StephenCurry handles injury. http://mashable.com/2016/04/25/steph-curry-knee-injury-cries-cried/"
];


let url = /\/\/.+?\.com?/;

tweets.forEach(function(tweet) {
  console.log(url.exec(tweet));
});

【问题讨论】:

标签: javascript


【解决方案1】:

使用new URL() 构造函数为您的查询创建一个快速脚本。

它会按单词拆分您的推文并对其进行测试。找到 URL 后,将填充 urls 数组。

let tweets = [
       "Thank you to the Academy and the incredible cast & crew of #TheRevenant. #Oscars",
       "@HardingCompSci department needs student volunteers for #HourOfCode https://hourofcode.com/us",
       "Checkout the most comfortable earbud on #Kickstarter and boost your #productivity https://www.kickstarter.com/",
       "Curious to see how #StephenCurry handles injury. http://mashable.com/2016/04/25/steph-curry-knee-injury-cries-cried/"
    ];
 
let urls = []
 
function getURL(me){
  me.split(" ").forEach(function(e){
    try { 
      new URL(e);
      console.log(e + " is a valid URL!")
      urls.push(e)
    } 
    catch (error){
      console.log(error.message);
    }
  })

}

tweets.forEach(function(tweet){
  getURL(tweet)
})

url.innerHTML = urls.join("<br>")
&lt;div id="url"&gt;&lt;/div&gt;

【讨论】:

    【解决方案2】:

    使用Capturing Group

    模式的一部分可以用括号 (...) 括起来。这称为“捕获组”。

    这有两个效果:

    它允许将匹配的一部分作为结果数组中的单独项目获取。 如果我们在括号后面加上一个量词,它就适用于整个括号。

    在您的代码中,您有 let url = /\/\/.+?\.com?/;

    您只对 2 个斜杠后面的部分感兴趣,因此通过将其括在大括号中来为其创建一个捕获组:let url = /\/\/(.+?\.com?)/;

    然后稍微更改循环中的代码以从第一个捕获组中获取结果,最终得到:

    let tweets = [
      "Thank you to the Academy and the incredible cast & crew of #TheRevenant. #Oscars",
      "@HardingCompSci department needs student volunteers for #HourOfCode https://hourofcode.com/us",
      "Checkout the most comfortable earbud on #Kickstarter and boost your #productivity https://www.kickstarter.com/",
      "Curious to see how #StephenCurry handles injury. http://mashable.com/2016/04/25/steph-curry-knee-injury-cries-cried/"
    ];
    
    
    let url = /\/\/(.+?\.com?)/;
    
    tweets.forEach(function(tweet) {
      var match = url.exec(tweet)
      console.log(match && match[1] || match);
    });

    【讨论】:

    • 我使用match &amp;&amp; match[1] || match 尽可能匹配原始代码。如果是我的代码,我会写 match &amp;&amp; match[1] 并让第一次迭代返回 undefined。
    猜你喜欢
    • 2018-09-08
    • 1970-01-01
    • 2011-05-06
    • 2017-10-16
    • 2011-11-05
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多