gpt4 book ai didi

javascript - 正则表达式匹配双引号内的每个字符串并包含转义引号

转载 作者:行者123 更新时间:2023-12-04 13:07:50 30 4
gpt4 key购买 nike

已经有很多类似的问题,但在我的情况下没有一个有效。我有一个在双引号内包含多个子字符串的字符串,这些子字符串可以包含转义的双引号。
例如对于字符串 '然后,“这是一些带有引号和\”转义引号\”的示例文本”。并不是说我们需要更多,而是……“这是“另一个”。以防万一。' ,预期结果是一个有两个元素的数组;

  • "this is some sample text with quotes and \"escaped quotes\" inside"
  • "here is \"another\" one"
  • /"(?:\\"|[^"])*"/g正则表达式在 regex101 上按预期工作;但是,当我使用 String#match()结果是不同的。看看下面的片段:

    let str = 'And then, "this is some sample text with quotes and \"escaped quotes\" inside". Not that we need more, but... "here is \"another\" one". Just in case.'
    let regex = /"(?:\\"|[^"])*"/g

    console.log(str.match(regex))

    我得到了四个,而不是两个匹配,并且转义引号内的文本甚至不包括在内。
    MDN mentions如果 g使用 flag 时,将返回所有匹配完整正则表达式的结果,但不会返回捕获组。如果我想获取捕获组并设置了全局标志,我需要使用 RegExp.exec() .我试过了,结果是一样的:

    let str = 'And then, "this is some sample text with quotes and \"escaped quotes\" inside". Not that we need more, but... "here is \"another\" one". Just in case.'
    let regex = /"(?:\\"|[^"])*"/g
    let temp
    let matches = []

    while (temp = regex.exec(str))
    matches.push(temp[0])

    console.log(matches)

    我怎样才能得到一个包含这两个匹配元素的数组?

    最佳答案

    另一种选择是没有 | 的更优化的正则表达式。运算符(operator):

    const str = String.raw`And then, "this is some sample text with quotes and \"escaped quotes\" inside". Not that we need more, but... "here is \"another\" one". Just in case.`
    const regex = /"[^"\\]*(?:\\[\s\S][^"\\]*)*"/g
    console.log(str.match(regex))

    使用 String.raw ,不需要两次转义引号。
    regex proof .顺便说一句, 28 steps267 steps .
    解释
    --------------------------------------------------------------------------------
    " '"'
    --------------------------------------------------------------------------------
    [^"\\]* any character except: '"', '\\' (0 or more
    times (matching the most amount possible))
    --------------------------------------------------------------------------------
    (?: group, but do not capture (0 or more times
    (matching the most amount possible)):
    --------------------------------------------------------------------------------
    \\ '\'
    --------------------------------------------------------------------------------
    [\s\S] any character of: whitespace (\n, \r,
    \t, \f, and " "), non-whitespace (all
    but \n, \r, \t, \f, and " ")
    --------------------------------------------------------------------------------
    [^"\\]* any character except: '"', '\\' (0 or
    more times (matching the most amount
    possible))
    --------------------------------------------------------------------------------
    )* end of grouping
    --------------------------------------------------------------------------------
    " '"'

    关于javascript - 正则表达式匹配双引号内的每个字符串并包含转义引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68568137/

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