gpt4 book ai didi

javascript - 正则表达式获取所有不是单引号的引号

转载 作者:行者123 更新时间:2023-11-30 15:28:31 25 4
gpt4 key购买 nike

是否可以使用一个正则表达式获取所有引用的文本?

来自正则表达式的示例文本:

Edit the "Expression" & Text to see matches. Roll over "matches" or the expression for details. Undo mistakes with ctrl-z. Save 'Favorites & "Share" expressions' with friends or the Community. "Explore" your results with Tools. A full Reference & Help is available in the Library, or watch the video Tutorial.

在这种情况下,我想捕获 ExpressionmatchesExplore 但不是 Share,因为 '收藏夹和“分享”表达式' 是单引号。

最佳答案

你不能在 Javascript 中构建一个只匹配你想要的部分的正则表达式,但是你可以构建一个模式来匹配所有字符串而没有间隙,并使用捕获组来提取你想要的部分:

/[^"']*(?:'[^']*'[^"']*)*"([^"]*)"/g
#^----------------------^ all that isn't content between double quotes

因为你的字符串可能以类似abcd 'efgh "ijkl"mnop' qrst 的结尾(简而言之,没有你想要的部分,但在单引号子字符串中有双引号部分),将模式更改为更安全:

/[^"']*(?:'[^']*(?:'[^"']*|$))*(?:"([^"]*)"|$)/g

并丢弃最后一场比赛。

关于javascript - 正则表达式获取所有不是单引号的引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42589520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com