Javascript 抓取网站链接后删除不必要的字符串。-Discord.JS

Javascript 抓取网站链接后删除不必要的字符串。-Discord.JS,javascript,node.js,discord.js,roblox,Javascript,Node.js,Discord.js,Roblox,这是我第一次在StackOverflow上发帖,所以我为我所犯的任何错误道歉 我想删除不必要的话后,你从一个不同的渠道链接 迄今为止的代码: message.content = message.content.toLowerCase(); let checker = 'roblox.com/games/59' if(message.content.search(checker) >= 8){ let embed = new discord.RichE

这是我第一次在StackOverflow上发帖,所以我为我所犯的任何错误道歉

我想删除不必要的话后,你从一个不同的渠道链接

迄今为止的代码:

    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 })
    }
这是抓取链接后的输出

Grabbed: https://www.roblox.com/games/5924680090/chromosome-ss-Place this my game it cool
我想删除以下字符串,使其只保留链接

this my game it cool

想法?

此示例
字符串
每个空格中分离出来,并从中创建一个
数组。然后
.slice()

const grabbedLink=”https://www.roblox.com/games/5924680090/chromosome-ss-Place 这是我的游戏,很酷”
const onlyLink=grabbedLink.trim().split(“”).slice(0,1);

console.log(onlyLink)
此示例每个空格处分离
字符串
,并从中创建一个
数组
。然后
.slice()

const grabbedLink=”https://www.roblox.com/games/5924680090/chromosome-ss-Place 这是我的游戏,很酷”
const onlyLink=grabbedLink.trim().split(“”).slice(0,1);

console.log(onlyLink)您可能可以使用regex进行此操作,并检查是否存在类似URL的字符串。正则表达式
((https?:\/\/)|(www\)[^\s]+)
检查字符串是否包含
http
https
www
,使用该方法可以将所有匹配的字符串抓取到一个数组中

请尝试以下代码段:

const content=“噢,https://roblox.com/games/5924680090/chromosome-ss-Place 这是我的游戏,很酷。另一个很酷的链接:https://stackoverflow.com :)"
const urlRegex=/((https?:\/\/)|(www\)[^\s]+)/gi
常量URL=content.match(urlRegex)
const robloxLink=URL&&URL[0]

log({URL,robloxLink})
您可能可以使用regex进行此操作,并检查是否存在类似URL的字符串。正则表达式
((https?:\/\/)|(www\)[^\s]+)
检查字符串是否包含
http
https
www
,使用该方法可以将所有匹配的字符串抓取到一个数组中

请尝试以下代码段:

const content=“噢,https://roblox.com/games/5924680090/chromosome-ss-Place 这是我的游戏,很酷。另一个很酷的链接:https://stackoverflow.com :)"
const urlRegex=/((https?:\/\/)|(www\)[^\s]+)/gi
常量URL=content.match(urlRegex)
const robloxLink=URL&&URL[0]

console.log({url,robloxLink})
如果文本位于链接前面怎么办。这是我的游戏,它很酷@systemGene在这种情况下你可以使用。我更新了我的答案,并在链接前面添加了一个空格,使用
.trim()
如果文本位于链接前面,它会起作用。在这种情况下,您可以使用我的游戏it cool@systemGene。我更新了我的答案,并在链接前面添加了一个空格,使用
.trim()
它可以工作一个简单的解决方案是在
message.content
中拆分字符串,然后切片并只保留第一项。您甚至可以使用Array.shift。一个简单的解决方案是在
message.content
中分割字符串,并只保留第一项。您甚至可以使用Array.shift。