Skip to content

Instantly share code, notes, and snippets.

@dieseltravis
Last active November 14, 2018 23:07
Show Gist options
  • Select an option

  • Save dieseltravis/138964 to your computer and use it in GitHub Desktop.

Select an option

Save dieseltravis/138964 to your computer and use it in GitHub Desktop.
html escaping unicode entities with their numeric values: Javascript, VB.Net, C#
html = html.replace(/[\u0080-\uFFFF]/g, function($0) {
return "&#" + ($0).charCodeAt(0) + ";";
});
' VB.net 2.0
Public Shared Function GetHtmlEntity(ByVal inputMatch As Match) As String
Return "&#" & AscW(inputMatch.Value).ToString() & ";"
End Function
Public Shared Function EscapeUnicodeEntities(ByVal html As String) As String
Dim escChars As New MatchEvaluator(AddressOf GetHtmlEntity)
html = Regex.Replace(html, "[\u0080-\uFFFF]", escChars, RegexOptions.Compiled)
Return html
End Function
// C# 3.5
html = Regex.Replace(html, @"[\u0080-\uFFFF]",
m => @"&#" + ((int)m.Value[0]).ToString() + ";", RegexOptions.Compiled);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment