gpt4 book ai didi

javascript - 正则表达式匹配对象数组中的键值

转载 作者:行者123 更新时间:2023-11-30 20:44:55 26 4
gpt4 key购买 nike

使用 https://regexr.com/我一直在尝试匹配数据对象中的部分。

json

{"unit":{"id":1,"val":"px","name":"px"},"type":{"id":3,"val":"HORIZONTAL_BAR","name":"Horizontal Bar"},"zoom":{"id":2,"val":"DEFAULT","name":"Show"},"scale":{"id":2,"val":"TRUE","name":"Show"},"fullscreen":{"id":1,"val":"FALSE","name":"Hide"},"street":{"id":2,"val":"TRUE","name":"Show"},"poi":{"id":1,"val":"FALSE","name":"Disable"},"draggable":{"id":2,"val":"TRUE","name":"Enable"},"doubleclick":{"id":2,"val":"TRUE","name":"Enable"},"mouse":{"id":2,"val":"TRUE","name":"Enable"},"gesture":{"id":2,"val":"auto","name":"Auto"},"map":{"id":1,"val":"ROADMAP","name":"Road Map"},"mapid":"mapkit-9479","api":"","lat":"51.343695608206275","lng":"-2.4871419408111706","zoomlvl":13,"width":600,"height":400,"position":{},"route":{},"infowindow":{"enable":false,"title":false,"tel":false,"email":false,"web":false,"desc":false},"visible":1,"lon":"-2.4871419408111706","user_id":4575,"map_url":"26T9Te","snazzy":{"id":12,"snazzy_id":11,"name":"Blue","json":"[{\"featureType\":\"all\",\"stylers\":[{\"hue\":\"#0000b0\"},{\"invert_lightness\":"true"},{\"saturation\":-30}]}]"}}

这是一个 json 的例子,如果你要检查它,它会失败,主要是因为这个片段。

{\"invert_lightness\":"true"}

我正在尝试做的是通过使用 REGEX 来匹配和替换来解决问题

我试过 \\"(.*)\\":"(.*)" 因为我认为它会匹配任何没有斜杠的 "我可以用斜线替换它。所以它看起来像 {\"invert_lightness\":\"true\"} 并修复它。

但是我写的正则表达式查询以一个匹配结束:

\"featureType\":\"all\",\"stylers\":[{\"hue\":\"#0000b0\"},{\"invert_lightness\":"true"},{\"饱和度\":-30}]}]"

最佳答案

看来你可以使用

\\"(.*?)\\"

并将其替换为

"$1"

参见 a demo on regex101.com .


这表示:

\\"   # match one backslash and "
(.*?) # capture 0+ characters in between, lazily into group 1
\\" # same as above

关于javascript - 正则表达式匹配对象数组中的键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48812668/

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