Skip to content

Instantly share code, notes, and snippets.

@trvswgnr
Created December 29, 2023 20:30
Show Gist options
  • Save trvswgnr/cb84ab710a81b30ec9323088dcbc907b to your computer and use it in GitHub Desktop.
Save trvswgnr/cb84ab710a81b30ec9323088dcbc907b to your computer and use it in GitHub Desktop.
escape html in a string - typescript
export function escapeHtml(str: string) {
let html = '';
let matchIndex: number | undefined;
let lastIndex = 0;
let char: string;
while (true) {
matchIndex = regex.exec(str)?.index;
if (matchIndex === undefined) break;
html += str.slice(lastIndex, matchIndex);
char = str[matchIndex];
html += escapeMap[char];
lastIndex = matchIndex + 1;
}
html += str.slice(lastIndex);
return html
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment