HTML द्वारा सुरक्षित वर्णों को बचाना चाहिए: हम HTML, XHTML या XML में केवल ASCII वर्णों का उपयोग करके किसी भी यूनिकोड चरित्र [Ex: & - U + 00026] का प्रतिनिधित्व करने के लिए एक चरित्र से बच का उपयोग कर सकते हैं। संख्यात्मक वर्ण संदर्भ [ Ex: एम्परसेंड (&) - &] और नामांकित वर्ण संदर्भ [Ex: &] प्रकार हैं character escape used in markup।
Original Character XML entity replacement XML numeric replacement
< < <
> > >
" " "
& & &
' ' '
HTML टैग को वेब पेज में एक सामान्य रूप के रूप में प्रदर्शित करने के लिए, जिसका हम उपयोग करते हैं <pre>, <code>टैग करते हैं या हम उनसे बच सकते हैं। स्ट्रिंग द्वारा "&"चरित्र की "&"किसी भी घटना और स्ट्रिंग द्वारा चरित्र की किसी भी घटना के साथ प्रतिस्थापित करके स्ट्रिंग ">"को बचाना ">"। उदाहरण के लिए:stackoverflow post
function escapeCharEntities() {
var map = {
"&": "&",
"<": "<",
">": ">",
"\"": """,
"'": "'"
};
return map;
}
var mapkeys = '', mapvalues = '';
var html = {
encodeRex : function () {
return new RegExp(mapkeys, 'g');
},
decodeRex : function () {
return new RegExp(mapvalues, 'g');
},
encodeMap : JSON.parse( JSON.stringify( escapeCharEntities () ) ),
decodeMap : JSON.parse( JSON.stringify( swapJsonKeyValues( escapeCharEntities () ) ) ),
encode : function ( str ) {
var encodeRexs = html.encodeRex();
console.log('Encode Rex: ', encodeRexs);
return str.replace(encodeRexs, function(m) { console.log('Encode M: ', m); return html.encodeMap[m]; });
},
decode : function ( str ) {
var decodeRexs = html.decodeRex();
console.log('Decode Rex: ', decodeRexs);
return str.replace(decodeRexs, function(m) { console.log('Decode M: ', m); return html.decodeMap[m]; });
}
};
function swapJsonKeyValues ( json ) {
var count = Object.keys( json ).length;
var obj = {};
var keys = '[', val = '(', keysCount = 1;
for(var key in json) {
if ( json.hasOwnProperty( key ) ) {
obj[ json[ key ] ] = key;
keys += key;
if( keysCount < count ) {
val += json[ key ]+'|';
} else {
val += json[ key ];
}
keysCount++;
}
}
keys += ']'; val += ')';
console.log( keys, ' == ', val);
mapkeys = keys;
mapvalues = val;
return obj;
}
console.log('Encode: ', html.encode('<input type="password" name="password" value=""/>') );
console.log('Decode: ', html.decode(html.encode('<input type="password" name="password" value=""/>')) );
O/P:
Encode: <input type="password" name="password" value=""/>
Decode: <input type="password" name="password" value=""/>