3 * Copyright(c) 2012-2013 TJ Holowaychuk
4 * Copyright(c) 2015 Andreas Lubbe
5 * Copyright(c) 2015 Tiancheng "Timothy" Gu
16 var matchHtmlRegExp = /["'&<>]/;
23 module.exports = escapeHtml;
26 * Escape special characters in the given string of html.
28 * @param {string} string The string to escape for inserting into HTML
33 function escapeHtml(string) {
34 var str = '' + string;
35 var match = matchHtmlRegExp.exec(str);
46 for (index = match.index; index < str.length; index++) {
47 switch (str.charCodeAt(index)) {
67 if (lastIndex !== index) {
68 html += str.substring(lastIndex, index);
71 lastIndex = index + 1;
75 return lastIndex !== index
76 ? html + str.substring(lastIndex, index)