Skip to content

Instantly share code, notes, and snippets.

@think49
Last active December 9, 2015 08:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save think49/1032896 to your computer and use it in GitHub Desktop.
Save think49/1032896 to your computer and use it in GitHub Desktop.
encodeHtmlEntity.js : <>&"' をHTMLエンティティ化する
/**
* encode-html-entity.js
* encode Character entity reference & Numeric character reference.
*
* @version 1.0.3
* @author think49
* @url https://gist.github.com/1032896
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License)
* @see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/named-character-references.html">11.5 Named character references - HTML Standard</a>
* @see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/apis-in-html-documents.html#innerhtml">3.3 APIs in HTML documents - HTML Standard</a>
*/
var encodeHtmlEntity = (function (String) {
function encodeHtmlEntity (string) {
return String(string).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\x22/g, '&quot;').replace(/\x27/g, '&#39;')
}
return encodeHtmlEntity;
}(String));
@think49
Copy link
Author

think49 commented Dec 9, 2015

ver 1.0.3

  • IE8 は &apos; に未対応だったのでシングルクォートは &#39; に書き換えるように修正しました。
  • 単純な文字列置換にしました。
  • 「半角空白 -> no break space」の置換処理を削除しました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment