Douglas Crockford: entityify & deentityify

2356 단어 entity
대신의 문자와 문자 실체의 상호 변환 방법
// & to &

if (!String.prototype.entityify) {

    String.prototype.entityify = function () {

        return this.replace(/&/g, "&").

                    replace(/</g, "&lt;").

                    replace(/>/g, "&gt;").

                    replace(/\"/g, "&quot;");

    };

}



// &amp; to &

if (!String.prototype.deentityify) {

    String.prototype.deentityify = function () {

        return this.replace(/&amp;/g, "&").

                    replace(/&lt;/g, "<").

                    replace(/&gt;/g, ">").

                    replace(/&quot;/g, "\"");

    };

}



var sourceStr = '<p>"a&b"</p>';



var entityStr = sourceStr.entityify();

console.log(entityStr);  // &lt;p&gt;&quot;a&amp;b&quot;&lt;/p&gt; 



var deentityStr = entityStr.deentityify();

console.log(deentityStr);  // <p>"a&b"</p>

좋은 웹페이지 즐겨찾기