【问题标题】:Regex to match the data between the double quotes正则表达式匹配双引号之间的数据
【发布时间】:2021-10-01 04:09:02
【问题描述】:

我有以下字符串

1. "Settings Icon": "xxhdjxxx7wn"
2. "Settings Icon": "afewfwl"
3. "Settings Icon": "fdj ssde"

我希望正则表达式通过忽略冒号右侧的内容来匹配所有这些字符串

我有(['"])(?:(?!\1|\\).|\\.)*\1,它与双引号内的内容相匹配。但无法解决上述情况..请帮助

【问题讨论】:

  • "(.*)": 这样的东西,就拿第1组吧,它应该给你左边。

标签: javascript regex


【解决方案1】:

如果我理解你的问题是正确的,你只需要引号内的字符串,从冒号的左侧开始。那是对的吗?如果是这样,这可能对您有用:

(['"])([^\1]*?)\1:.*$

对于这个表达式,捕获组 2 会给你你想要的。

【讨论】:

    【解决方案2】:

    使用

    const string = `1. "Settings Icon": "xxhdjxxx7wn"
    2. "Settings Icon": "afewfwl"
    3. "Settings Icon": "fdj ssde"`
    
    console.log( string.match(/(?<=")[^"]+(?=":)/g) )

    演示

    解释

    --------------------------------------------------------------------------------
      (?<=                     look behind to see if there is:
    --------------------------------------------------------------------------------
        "                        '"'
    --------------------------------------------------------------------------------
      )                        end of look-behind
    --------------------------------------------------------------------------------
      [^"]+                    any character except: '"' (1 or more times
                               (matching the most amount possible))
    --------------------------------------------------------------------------------
      (?=                      look ahead to see if there is:
    --------------------------------------------------------------------------------
        ":                       '":'
    --------------------------------------------------------------------------------
      )                        end of look-ahead
    

    regex proof

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-13
      • 1970-01-01
      相关资源
      最近更新 更多