Characters under 0x20 aren’t shown, so I would like to escape them as ‘\x0D’, for example.
Currently, I’m using:
str = str.replace(/([\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f])/,function(b){
var b = b.charCodeAt();
return '\\x'+(b<16?'0':'')+b.toString(16).toUpperCase();
});
Is there a better way? Originally, I was doing:
str = escape(str).replace('%(\d\d)','\\x$1');
..but it escaped characters with values above 32.
Ranges.
Also note I removed the parentheses – use
$&to get the whole pattern, instead of$1.