I'm trying to decode URLs that may be single or double percent encoded using decodeURIComponent()
. Because I don't want to get an "URI malformed"
when a string is attempted to be decoded that only contains %25
(5), I'm using this try/catch
block. Have I used the try/catch
block correctly? It seems to work, but I'm trying to understand if this is a best practice approach, or if I've over-complicated things.
var uriCleanup = function (str) {
var cln = function (str) {
try {
str = decodeURIComponent(str);
//try to decode the URI again
return cln(str);
} catch (e) {
//if error thrown, URI contains single percent symbol
//return the str as it was the last time it was decoded
return str;
}
};
return cln(str);
};