Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取包含特定单词的所有链接_Javascript_Regex_Discord_Discord.js - Fatal编程技术网

Javascript 获取包含特定单词的所有链接

Javascript 获取包含特定单词的所有链接,javascript,regex,discord,discord.js,Javascript,Regex,Discord,Discord.js,我正在尝试获取所有包含“discord”字样的链接,比如“https://discord.com“,”www.discordapp.com“,”discord.me“等 我目前的代码有点工作,因为我正在寻找一些可以用更少的步骤完成的东西(比如删除isDiscordInvite函数) 这是mt当前代码: const getUrls = (str: string) => { const regex = /(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:

我正在尝试获取所有包含“discord”字样的链接,比如“https://discord.com“,”www.discordapp.com“,”discord.me“等

我目前的代码有点工作,因为我正在寻找一些可以用更少的步骤完成的东西(比如删除
isDiscordInvite
函数)

这是mt当前代码:

const getUrls = (str: string) => {
  const regex = /(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[A-Z0-9+&@#/%=~_|$])/gim;
  return [...str.matchAll(regex)].map((url) => url[0]);
};

const isDiscordInvite = (url: string) => {
  const isServerInvite = /discord/.test(url);

  return isServerInvite;
};

export const getDiscordInvitesFromMessage = (message: string) => {
  const urls = getUrls(message);

  const invites = urls.filter(isDiscordInvite);
};
有人知道一个正则表达式可以获取包含“discord”的所有链接吗

正确回报的例子
const exampleInput=“这是一个输入https://discord.com"
const arrayOfUrls=getDiscordUrls(exampleInput)//执行以下操作
//arrayOfUrls应具有此值,且仅此值
//阵列卷曲===[”https://discord.com"]
Obs1:我需要一种方法**获取链接**,然后与其他内容进行比较,而不仅仅是知道消息中是否有邀请

Obs2:链接必须直接从文本中提取,而不是从锚元素中提取

例如:

arrayOfUrls.filter(allUrlsStartsWithHttps)
//或
arrayOfUrls[0]=“www.google.com”
使用

/(?:(?:https?| ftp |文件):\/\/\/| www\.| ftp\)[^\s/]*不和谐\s*/gi

解释

--------------------------------------------------------------------------------
  (?:                      group, but do not capture:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture:
--------------------------------------------------------------------------------
      http                     'http'
--------------------------------------------------------------------------------
      s?                       's' (optional (matching the most
                               amount possible))
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      ftp                      'ftp'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      file                     'file'
--------------------------------------------------------------------------------
    )                        end of grouping
--------------------------------------------------------------------------------
    :                        ':'
--------------------------------------------------------------------------------
    \/                       '/'
--------------------------------------------------------------------------------
    \/                       '/'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    www                      'www'
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    ftp                      'ftp'
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
  )                        end of grouping
--------------------------------------------------------------------------------
  [^\s/]*                  any character except: whitespace (\n, \r,
                           \t, \f, and " "), '/' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  discord                  'discord'
--------------------------------------------------------------------------------
  \S*                      non-whitespace (all but \n, \r, \t, \f,
                           and " ") (0 or more times (matching the
                           most amount possible))

你为什么用正则表达式。如果只想获取与单词use String.contains()匹配的所有内容,请获取URL作为字符串,然后检查字符串。这些URL来自哪里?什么意思是“能够与其他内容进行比较”?是的,解析文本,如“这是一个邀请”,函数应该返回一个数组,如
[”https://discord.com“]
(使用完整链接,并且仅使用完整链接)。不正确的返回:`[“https”、“@RokoC.Buljan类似这样:``arrayOfUrls.filter(allUrlsStartWithHttps)或arrayOfUrls[0]===“www.google.com”```@RenatoRazal和您当前的代码有什么问题吗?有效吗?遗漏了什么?不起作用吗?谢谢!这是一个如此完整和难以置信的答案,如果可以的话,我会给您2”正确答案" hahaha@RenatoRazal一个就够了,祝你一天过得愉快!