Last active
November 14, 2018 23:07
-
-
Save dieseltravis/138964 to your computer and use it in GitHub Desktop.
html escaping unicode entities with their numeric values: Javascript, VB.Net, C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| html = html.replace(/[\u0080-\uFFFF]/g, function($0) { | |
| return "&#" + ($0).charCodeAt(0) + ";"; | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ' 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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