从 url javascript 中提取域名

时间:2021-01-07 04:14:37

标签: javascript

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

2 个答案:

答案 0 :(得分:1)

使用 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);
});

答案 1 :(得分: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>