简体   繁体   中英

delete specific quotes in javascript

Now I am gonna to edit this :

var coordinates = [
         {"lat": 37.772, "lng": -122.214, "status": "walking"},
         {"lat": 36.772, "lng": -123.214, "status: "walking"}
         ];

And I use the replace function like this:

JSON.stringify(data).replace(/\"/g, "");

But after I replace the double quotes to "", the value walking's double quotes also got deleted, but I want the value walking can keep the quotes so js could regard it as string.

You can grab all properties and remove quotes around it with this regex: /"([az]*?)":/ig . Keeping the string values with quotes.

var coordinates = [
     {"lat": 37.772, "lng": -122.214, "status": "walking"},
     {"lat": 36.772, "lng": -123.214, "status": "walking"}
];

var x = JSON.stringify(coordinates);

x.replace(/"([a-z]*?)":/ig, (m, m1) => m1 + ':');

Before

[{"lat":37.772,"lng":-122.214,"status":"walking"},{"lat":36.772,"lng":-123.214,"status":"walking"}]

After

[{lat:37.772,lng:-122.214,status:"walking"},{lat:36.772,lng:-123.214,status:"walking"}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM