Node.js Bot从消息中提取链接

Node.js Bot pulling links out of messages

本文关键字:提取 链接 消息 js Bot Node      更新时间:2023-09-26

我正在node.js中为Discordapp.com制作一个机器人程序,它将从steam配置文件的xml中提取信息。它从用户消息中获取配置文件链接。

我很难找到一种方法让我的机器人从消息中提取链接,因为它们不一致。

消息可能看起来像:

http://steamcommunity.com/profiles/76562119803123205611 look at that guys

Take a look at this www.steamcommunity.com/id/someusername/

look at this steamcommunity.com/profiles/766119803232061 looks cool right?

唯一一致的是"http://steamcommunity.com/"其他的都不一样。我能得到一些帮助吗?如何从每条消息中只提取整个链接,而不提取其他链接?

这是正则表达式的好地方。这是一个匹配任何steamcommunity.com URL的方法,其中/profiles/后跟数字,/id/后跟字符。

var test = "www.steamcommunity.com/id/someusername/";
var regexp = new RegExp("(steamcommunity.com)(/id/[a-z]+|(/profiles/('d)+))", "i");
var out = test.match(regexp)[0];