Your string doesn't have any literal backslashes in it. For example, \tAgile,Scrum
is equivalent to a tab character concatenated with Agile,Scrum
. There are no backslashes in the string, so /\\/g
, which matches a literal backslash, will not match anything.
You'll have to match the interpreted character(s) instead, like \t
, and replace them with a literal backslash followed by a literal t
:
var a = {"value": '[{"skill":"\tAgile,Scrum", "exp": 2}, {"skill": "Coding", "exp": 10 }]'};
a.value = a.value.replace(/\t/g, "\\t"); //handle special char
console.log(a);
You'll have to write out each character you want to replace. If you have a bunch of them (eg \t
\r
\n
\v
etc) you might find it more concise to start with an object mapping the characters to their replacement values and constructing the pattern from that, eg:
const replacements = {
'\t': '\\t',
'\r': '\\r',
// etc
};