Inspired by this regex-based answer to a unicode question I now have following javascript code, which unescapes unicoded encoded character occurrences in strings.
var testString = "\\u53ef\\u4ee5NonUnicode\\u544a\\u8bc9\\u6211";
print(testString)
String.prototype.unescape = function() {
return this.replace(/\\u([0-9a-f]{4})/g,
function (whole, group1) {
return String.fromCharCode(parseInt(group1, 16));
}
);
};
print(testString.unescape()) // outputs: 可以NonUnicode告诉我
I could not find a way in Java (1.7) to do this kind of dynamic regex replacement, there are only static approaches like java.lang.String.replaceAll or java.util.regex.Matcher.group, which returns the group, but has no means to set it.
Is this even possible in Java? Are there any workarounds?