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

  1. convert json string using json.stringify()
  2. use replace on string
  3. convert string json using json.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

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -