Question

Is there a way to parse utf codes in vbscript? What I'd like to do is replace all codes like "\u00f1" in a string for its corresponding character.

Was it helpful?

Solution

The Unescape function does that*, only it requires that the Unicode characters are encoded in the %u***xxxx* format. So, you'll need to replace the \u***xxxx* codes with their **%u***xxxx* equivalents first. Here's an example:

str = "\u0044\u006F \u0063\u0061\u0074\u0073 \u0065\u0061\u0074 \u0062\u0061\u0074\u0073\u003f"

Set re = New RegExp
re.Pattern = "\\(u[a-f\d]{4})"
re.IgnoreCase = True
re.Global = True

str2 = Unescape(re.Replace(str, "%$1"))
MsgBox str2

* Note that Unescape also replaces the %***xx* codes in the string with the corresponding ASCII characters. So, if %***xx* is a legal substring in your string, you'll have to write your own replacement function. Such a function could do the following:

  • search for occurences of the **\u***xxxx*-like substrings in your input string,
  • extract the character code from each match, and convert it from hexadecimal to decimal form,
  • call ChrW to convert the decimal character code to the corresponding Unicode character,
  • replace each **\u***xxxx* match with the coresponding character.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top