I have a String:
c:\build\target
.
This es from an external source, passed directly as it is. I cannot modify it before using it in a javascript.
However \b
and \t
are special characters, but I'd simply like to print it (or work with it) as it looks like, not as it is interpreted c:uild arget
.
How is it possible?
I have a String:
c:\build\target
.
This es from an external source, passed directly as it is. I cannot modify it before using it in a javascript.
However \b
and \t
are special characters, but I'd simply like to print it (or work with it) as it looks like, not as it is interpreted c:uild arget
.
How is it possible?
Share Improve this question edited Oct 26, 2012 at 14:57 Ash Burlaczenko 25.5k16 gold badges69 silver badges101 bronze badges asked Oct 26, 2012 at 14:54 Balázs NémethBalázs Németh 6,6679 gold badges47 silver badges65 bronze badges 6- parse through the string and add another '\' whenever you need to – Gene Parmesan Commented Oct 26, 2012 at 14:58
- @GeneParmesan, why regex for such a simple task? – Ash Burlaczenko Commented Oct 26, 2012 at 14:59
- @GeneParmesan - That was my first thought, but by the time the string exists in Javascript it already has no backslash characters to match with the regex! – Andrzej Doyle Commented Oct 26, 2012 at 15:02
- Too bad file systems don't just use '/' instead ... – Gene Parmesan Commented Oct 26, 2012 at 15:06
- If you are passing the string from the server to the client, does the client get the wrong string to begin with or is the string manipulated after? – Gene Parmesan Commented Oct 26, 2012 at 15:14
3 Answers
Reset to default 3Using a single regex replace (instead of many) is quicker and avoids errors related to the replace order.
console.info('c:\build\target'); // cuild arget
console.info(escape('c:\build\target')); // c:\build\target
function escape(str) {
return str.replace(/[\b\f\n\r\t\v\0\'\"\\]/g, match => {
return {
'\b': '\\b',
'\f': '\\f',
'\n': '\\n',
'\r': '\\r',
'\t': '\\t',
'\v': '\\v',
'\0': '\\0',
'\'': '\\\'',
'\"': '\\\"',
'\\': '\\\\'
}[match]
})
}
You could try:
string = "c:\build\target";
string = string.replace(/[\n]/g,'\\n');
string = string.replace(/[\r]/g,'\\r');
string = string.replace(/[\t]/g,'\\t');
string = string.replace(/[\b]/g,'\\b');
string = string.replace(/[\f]/g,'\\f');
console.log(string);
But there maybe some other problems, because you have to catch all the other special chars
The simplest way would be to escape every backslash (so it's interpreted as a literal backslash).
If you can't do this on the remote system itself, you'll need to do this as the data is being parsed in Javascript. Obviously once you already have a Javascript string, the backslashes are already missing.
So at the point where you go from "remote string" (which includes a \
character followed by a b
) to "Javascript string" (which includes a \b
unprintable character), you'll need to keep an eye out for backslashes. And ensure they're passed through as a literal backslash (i.e. escaped), rather than as an escape prefix.