Created
January 15, 2013 07:33
-
-
Save kawanet/4536936 to your computer and use it in GitHub Desktop.
This does unescape &#xHHHH; \uHHHH on HTML/JSON/YAML etc. An Unicode escape decoder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cache = {}; | |
var regex = /\\u[0-9a-f]{4}|\&(\w+|\#([0-9]+|x[0-9a-f]+))\;/g; | |
var ignore = /[\x00-\x20\>\<\&\"\']/; | |
var func = function (match) { | |
var ret = cache[match]; | |
if (! ret) { | |
var head = match.charAt(0); | |
if (head == "&") { | |
var p = document.createElement("p"); | |
p.innerHTML = match; | |
ret = p.firstChild ? p.firstChild.data : match; | |
} else { | |
var src = match.substr(2, 4); | |
var cd = parseInt(src, 16); | |
ret = String.fromCharCode(cd); | |
} | |
if (ret.match(ignore)) ret = match; | |
cache[match] = ret; | |
} | |
return ret; | |
}; | |
var walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT); | |
while(walker.nextNode()) { | |
var src = walker.currentNode.data; | |
if (! src) continue; | |
var dst = src.replace(regex, func); | |
if (src != dst) walker.currentNode.data = dst; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
unescape - ページ中の&#xHHHH;や\uHHHHのエスケープを展開するブックマークレット
http://kawanet.hatenablog.com/entry/20121120/1353402584