LoginSignup
0
0

More than 3 years have passed since last update.

HTMLエスケープ文字列のデコード用関数にしっくりくるものが無かったので、自分で作ってみた

Last updated at Posted at 2020-05-17

HTMLデコード

function decodeHTML(str) {
  return str.replace(/&(?:([a-z]+?)|#(\d+?));/g, function(m, c, d) {
    return c ? ({
      "amp": "&",
      "lt": "<",
      "gt": ">",
      "quot": '"',
      "nbsp": " "
    }[c] || m) : d ? String.fromCharCode(d) : m;
  });
}

decodeHTML("&lt;a onclick=&quot;hoge(&#039;fuga&#039;)&quot;&gt;")
// <a onclick="hoge('fuga')">
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0