【问题标题】:Javascript regex expression text between """""" 之间的 Javascript 正则表达式文本
【发布时间】:2020-03-23 06:50:35
【问题描述】:

如何在 javascript 的正则表达式中获取 """""" 之间的文本?

例子:

"""hello world""" 应该给hello world

""" hi """ """world""" 应该给[hi, world]

【问题讨论】:

  • 尝试"{3}((?:(?!"{3}).)*)"{3} - 使用trim 清理它。您也可以使用 "{3}\s*((?:(?!"{3}).)*?)\s*"{3} 而不使用 trim

标签: javascript regex string


【解决方案1】:

给你:

(?<=""") ?\w+ ?( \w+)*(?=""")

解释

(?<=""")   --->   make sure three quotes appear prior to a word
 ?         --->   optional space
\w+        --->   match any word characters
( \w+)*    --->   match 0 or more spaced out words
(?+""")    --->   ensure that there's following close quotes

看到它在行动here

【讨论】:

  • 发帖前应该多做测试:regex101.com/r/6wz0yF/2
  • @Toto 谢谢你让我注意到这一点,我承认有点尴尬,但我已经纠正了我的错误
  • 嗯,这样更好,但为什么要将字符限制为单词字符?这将不匹配 """I've bought a phone that costs $1000.00""""。并非所有浏览器都支持lookbehind。
  • @Toto 我将基于 OP 的示例字符串,如果您希望捕获三引号之间的所有内容,您可以使用此正则表达式:(?&lt;=""").+?(?=""") 作为通用解决方案。如果您有任何其他问题,请随时收件箱或发布您自己的问题,以避免向当前线程发送垃圾邮件:) 在此处查看实际操作:regex101.com/r/xz6oSq/2
  • 我没有任何问题,我只是告诉你,lookbehind 并不适用于所有浏览器,所以你的答案不能用于生产。
【解决方案2】:

不确定 OP 是否仍在寻找答案,但您应该尝试:

/"""([^"""]+)"""/g

这将匹配 """ hi """ """world""" """hello world""" 甚至 """I've bought a phone that costs $1000.00"""

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-16
    • 2016-11-18
    • 2012-02-22
    • 2023-04-07
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多