【问题标题】:Yup Regex Instagram hashtags是的,正则表达式 Instagram 主题标签
【发布时间】:2021-02-17 04:09:33
【问题描述】:

我一直在尝试使用 Formik 中的 Yup 查找/创建一个正则表达式来验证 Instagram 的标签和提及(用户句柄)。

我找到了一个post,这几乎是我需要的,但是,我的正则表达式生锈了,我似乎无法让它正常工作。

// Regex for hashtags
/^(?:#)([A-Za-z0-9_](?:(?:[A-Za-z0-9_]|(?:\.(?!\.))){0,28}(?:[A-Za-z0-9_]))?)/gm

// Within schema
hashtags: Yup.string()
  .required('Tags are required')
  .matches(
    /^(?:#)([A-Za-z0-9_](?:(?:[A-Za-z0-9_]|(?:\.(?!\.))){0,28}(?:[A-Za-z0-9_]))?)/gm
  )

目前正在发生的事情

#foo #bar
#foo,#bar
#foo#bar
#foo bar
#foo,bar

希望通过

#foo #bar

提前致谢

【问题讨论】:

  • @WiktorStribiżew 在 regex101 中运行良好,但是在 Yup.match 中无法正常运行。 #foo #bar 仍然失败,但是,#foo bar 通过。看起来这对于 Yup 来说比单独的正则表达式更具体。
  • Instagram 现在有带有表情符号的标签。这个正则表达式不会检测到它们

标签: javascript regex formik yup


【解决方案1】:

这就是我在 Yup 中的 instagram 标签。非常适合我们。 Regex101.

/^(?:#)([A-Za-z0-9_](?:(?:[A-Za-z0-9_]|(?:(?!))){0,28}(?:[A-Za-z0-9_]))?)((?: #)([A-Za-z0-9_](?:(?:[A-Za-z0-9_]|(?:\.(?!\.))){0,28}(?:[A-Za-z0-9_]))?))*$/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-20
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    • 2017-12-02
    相关资源
    最近更新 更多