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

swift - Button on Table View Cell connected to local function -

dns - Dokku server hosts two sites with TLD's, both domains are landing on only one app -

c# - ajax - How to receive data both html and json from server? -