javascript - String replace from JSON -
i have json response not formatted , has speical characters coming in , want clean removing special characters using string.replace(). reason not working.
here json result
[{"user::newpassword":["password not changed. request can't validated"]},[]]
and here expression.
resp.replace(::g, '');
but doesn't seem work. advice on appreciated there nothing can on backend.
you cannot use replace()
on json.
if you're working strings
::
should in quotes if want replace first occurrence of it.
resp.replace('::', '');
if want replace occurrences, use /
delimiter of regex.
resp.replace(/::/g, '');
if you're working json
- convert
json
string usingjson.stringify()
- use
replace
on string - convert
string
json
usingjson.parse()
using object methods
you can change key
remove ::
it
var newkey = key.replace('::', ''); // create new key replacing `::` obj[newkey] = obj[key]; // add new key , value in object delete key; // remove old key
Comments
Post a Comment