0

I'm getting passed a string from an API method I have no control over that returns strings with paths that sometimes that look like: /toplevel/nextlevel/_x0034_33name/myoutput.log

It seems to happens with directory names that start with a number. In this case the directory name should be '433name'.

I'm guessing that x0034 represents hex for the character '4', possibly in unicode.

The following Javascript returns '4', which would be correct:

String.fromCharCode(parseInt("0034",16))

Is there a regex command or conversion utility in Javascript readily available to remove and replace all these characters in the string with their correct equivalents?

George Hernando
  • 2,550
  • 7
  • 41
  • 61
  • 1
    possible duplicate of [How do I decode a string with escaped unicode?](http://stackoverflow.com/questions/7885096/how-do-i-decode-a-string-with-escaped-unicode) – Michal Brašna Feb 05 '14 at 23:15

3 Answers3

2
function unescapeApi(string) {
    return string.replace(/_x([\da-f]{4})_/gi, function(match, p1) {
        return String.fromCharCode(parseInt(p1, 16));
    });
}

# example, logs '/433name/myoutput.log'
console.log(unescapeApi('/_x0034_33name/myoutput.log'));
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
0

Your diagnostics are okay but a bit off. The 'encoded' part is not just the 'x', it's the entire string _xhhhh_.

Try this:

x = '/toplevel/nextlevel/_x0034_33name/myoutput.log';
y = x.replace (/_x([0-9A-F]{4})_/gi, function (a,b) { return String.fromCharCode(parseInt(b,16)); });

-- then y will hold your parsed path.

(Oh, as F.J. says, this might need the i Ignore Case flag as well. Hard to say with such a limited test set of data.)

Jongware
  • 22,200
  • 8
  • 54
  • 100
0
'/toplevel/nextlevel/_x0034_33name/myoutput.log'.replace(/_x[\da-f]{4}_/gi,function(match)  {
return String.fromCharCode(parseInt(match.substr(2,4),16)) 

});

This will be fine as long as the encodings match

Jon Cooke
  • 92
  • 3