You can create a custom function to replace all characters you need:
const replaceAll = (text, obj) => {
return [...text].map(each => {
for (const o in obj){
(each == o) ? each = obj[o] : o;
}
return each;
}).join('');
};
replaceAll('abc', {'a':'x', 'b':'y'}); //"xyc"
Here is another alternative using string.prototype
:
String.prototype.replaceAll = function(obj) {
let finalString = '';
let word = this;
for (let each of word){
for (const o in obj){
const value = obj[o];
if (each == o){
each = value;
}
}
finalString += each;
}
return finalString;
};
'abc'.replaceAll({'a':'x', 'b':'y'}); //"xyc"
var res = string.replace(/\T/g,' ').replace(/\Z/g,'');
– VadimB Jun 2 '17 at 9:05