Replace special characters in array in JavaScript

Advertisements

The easy way to replace special characters in an array of objects in JavaScript, is :

Transform the array to a json string.
Then apply a regex to replace characters.
Parse the json String to obtain a new array without the special characters.

For Exemple :

var obj = {
  'a': '\nThe fooman poured the drinks.',
  'b': {
    'c': '\tDogs say fook, but what does the fox say?'
  }
}

console.log(JSON.parse(JSON.stringify(obj).replace(/[\\n\\t]/g, '')));

Leave a Reply Cancel reply