Utilizando https://regexr.com/ he intentado hacer coincidir piezas dentro de un objeto de datos.

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}]}]"}}

Este es un ejemplo de json y si lo hiciera lint, fallaría principalmente debido a este segmento.

{\"invert_lightness\":"true"}

Lo que estoy tratando de hacer es usar REGEX es hacer coincidir y reemplazar para solucionar el problema

Probé \\"(.*)\\":"(.*)" porque pensé que coincidiría con cualquier " que no tenga barra y podría reemplazarlo con una barra. para que se vea como {\"invert_lightness\":\"true\"} y lo arregle.

Pero la consulta de expresiones regulares que escribí termina con una coincidencia:

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

1
ngDough 15 feb. 2018 a las 20:06

2 respuestas

La mejor respuesta

Parece que podrías usar

\\"(.*?)\\"

Y reemplaza esto con

"$1"

Consulte una demostración en regex101.com .


\\"   # match one backslash and "
(.*?) # capture 0+ characters in between, lazily into group 1
\\"   # same as above
1
Jan 15 feb. 2018 a las 17:48

Prueba esto:

str.replace(/\\"/g, '"').replace(/"\[/g, '[').replace(/\]"/, ']')
0
zelda11 15 feb. 2018 a las 17:29