简体   繁体   中英

How do I replaceAll character from json string using javascript

I have below json data I need to replace "=" to ":" by Javascript

{ "name"="John", "age"=30, "car"=null }

Expected output:

{ "name":"John", "age":30, "car":null }

This should do the trick:

var str = '{ "name"="John", "age"=30, "car"=null }';
str = str.replace(/=/g,":");

var json = JSON.parse(str);

Note, that it would convert ALL "=" to ":". If there can be symbol in name or value, different approach should be used.

-- Update "g" modifier has to be used if there is more than one "=" to replace.

使用g标志:

'{ "name"="John", "age"=30, "car"=null }'.replace(/\=/g, ':')

You can use Replace

 let op = `{ "name"="John", "age"=30, "car"=null }`.replace(/=/g, ':') console.log(op) 

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