从URL中提取域名的JavaScript

3

我试图从“推文”字符串中提取域名,如何避免从字符串中提取双反斜杠?我在 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));
});


1
这个回答解决了您的问题吗?在字符串中查找URL的正则表达式 - Yair Cohen
2个回答

1

使用捕获组

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

它有两个作用:

它允许将匹配的一部分作为结果数组中的单独项获取。 如果在括号后面放置量词,则整个括号都会受到影响。

在您的代码中,您有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 && match[1] || match 来尽可能地匹配原始代码。如果这是我的代码,我会写成 match && match[1] 并让第一次迭代返回未定义值。 - Stijn de Witt

0
制作了一个快速脚本来处理您的查询,使用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>")
<div id="url"></div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接