Skip to content

Instantly share code, notes, and snippets.

@zhgqthomas
Created December 8, 2017 07:35
Show Gist options
  • Save zhgqthomas/c9801b9294daae54592679918a27d29b to your computer and use it in GitHub Desktop.
Save zhgqthomas/c9801b9294daae54592679918a27d29b to your computer and use it in GitHub Desktop.

Revisions

  1. zhgqthomas created this gist Dec 8, 2017.
    8 changes: 8 additions & 0 deletions htmldecode.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    function htmlDecode(str) {
    // 有 x 则表示是16进制,$1 就是匹配是否有 x,$2 就是匹配出的第二个括号捕获到的内容,将 $2 以对应进制表示转换
    str = str.replace(/&#(x)?(\w+);/g, function($, $1, $2) {
    return String.fromCharCode(parseInt($2, $1? 16: 10));
    });

    return str;
    }