日期:2014-05-17  浏览次数:20615 次

对目标字符串进行html编码

?

有的时候我们需要对字符串进行编码,将对应的&<>"'这些进行编码

?

?

/*
encodeHTML --对目标字符串进行html编码
*@function*
*@param {String} source*
*@return {String} html编码后的字符串* ----对&<>"'进行编码
*/
ZYC.string.encodeHTML = function(source){
    return String(source).replace(/&/g,'&amp;')
	                     .replace(/</g,'&lt;')
						 .replace(/>/g,'&gt;')
						 .replace(/"/g,'&quot;')
						 .replace(/'/g,'&#39;')
};
?

?