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?

1

3 Answers 3

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'));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for a quick response! It tests out OK for me.
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.)

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.