【问题标题】:Get all links that contains a specific word in it获取其中包含特定单词的所有链接
【发布时间】:2021-03-11 22:57:00
【问题描述】:

我正在尝试获取所有包含“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);
};

有没有人知道一个正则表达式可以获取所有包含“不和谐”的链接?

正确返回示例

const exampleInput = "This is an input https://discord.com"

const arrayOfUrls = getDiscordUrls(exempleInput) // Do the magic

// arrayOfUrls should have this value and ONLY this value
// arrayOfUrls === ["https://discord.com"]

Obs1:我需要一种方法**获取链接**,以便与其他事物进行比较,而不仅仅是知道消息中是否有邀请。

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

例子:

arrayOfUrls.filter(allUrlsStartsWithHttps)
// OR
arrayOfUrls[0] === "www.google.com"

【问题讨论】:

  • 你为什么使用正则表达式。如果您只想获取与单词匹配的所有内容,请使用 String .contains(),将 URL 作为字符串获取,然后检查该字符串。这些网址来自哪里?
  • 什么是“能够与其他事物进行比较”
  • 是的,解析诸如“这是一个邀请discord.com”之类的文本,该函数应该返回一个数组,例如["https://discord.com"](带有完整链接,并且只有完整链接)。返回错误:`["https", "discord.com"]
  • @RokoC.Buljan 像这样:``` arrayOfUrls.filter(allUrlsStartWithHttps) OR arrayOfUrls[0] === "www.google.com" ```
  • @RenatoRazal 您当前的代码有什么问题?作品?错过了什么?不工作?

标签: javascript regex discord discord.js


【解决方案1】:

使用

/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[^\s/]*discord\S*/gi

proof

说明

--------------------------------------------------------------------------------
  (?:                      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))

【讨论】:

  • 谢谢!这是一个如此完整和令人难以置信的答案,如果可以的话,我会给你2个“正确答案”哈哈哈
  • @RenatoRazal 一个就够了,玩得开心!
猜你喜欢
  • 2014-09-23
  • 2023-04-09
  • 1970-01-01
  • 2012-01-02
  • 1970-01-01
  • 2015-05-21
  • 2015-04-01
  • 1970-01-01
相关资源
最近更新 更多