抓取网站后删除不必要的字符串 link.- Discord.JS

Removing unnecessary string after grabbing a website link.- Discord.JS

这是我第一次在 Whosebug 上发帖,对于我犯的任何错误,我深表歉意。

我想在从不同的频道 yoinking link 后删除不必要的词。

到目前为止的代码:

    message.content = message.content.toLowerCase();
    let checker = 'roblox.com/games/59'
    if(message.content.search(checker) >= 8){
        let embed = new discord.RichEmbed()
        .setTitle("**Possible condo found!**")
        .setColor(randomColor)
        .setDescription("Grabbed: "+ message.content +"")
        client.channels.get("809420315230339112").send({ embed: embed })
    }

这是抓取一个link后的输出。

Grabbed: https://www.roblox.com/games/5924680090/chromosome-ss-Place this my game it cool

我想删除以下字符串,这样它只留下 link。

this my game it cool

想法?

这个例子每个space分离 string并创建一个array它。然后 .slice() 删除 除了第一个 element.

const grabbedLink = " https://www.roblox.com/games/5924680090/chromosome-ss-Place this my game it cool"

const onlyLink = grabbedLink.trim().split(' ').slice(0, 1);

console.log(onlyLink);


参考文献:

您可以为此使用正则表达式并检查是否有看起来像 URL 的字符串。正则表达式 (((https?:\/\/)|(www\.))[^\s]+) 检查字符串是否包含 httphttpswww 并使用 .match() 方法可以将所有这些匹配的字符串放入一个数组中。

试试下面的代码片段:

const content = "Oh, https://roblox.com/games/5924680090/chromosome-ss-Place this my game it cool. Another cool link: https://whosebug.com :)"

const urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/gi
const urls = content.match(urlRegex)
const robloxLink = urls && urls[0]

console.log({ urls, robloxLink })

你也可以把它移到它自己的函数中,然后像这样使用它:

function findLinks(string) {
  const urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/gi
  return content.match(urlRegex)
}

const content = "Oh, https://roblox.com/games/5924680090/chromosome-ss-Place this my game it cool. Another cool link: https://whosebug.com :)"

console.log(findLinks(content))

或者,您可以使用像 get-urls.

这样的 npm 包