ut2
Version:
一个现代 JavaScript 实用工具库。[点击查看在线文档]。
22 lines (19 loc) • 515 B
JavaScript
import toString from './toString.js';
var htmlEscapes = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
var reUnescapedHtml = /[&<>"']/g;
var reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
function escape(string) {
string = toString(string);
return string && reHasUnescapedHtml.test(string)
? string.replace(reUnescapedHtml, function (chr) {
return htmlEscapes[chr];
})
: string || '';
}
export { escape as default };