views:

77

answers:

2

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.

+1  A: 

The Unescape function does that*, only it requires that the Unicode characters are encoded in the %uxxxx format. So, you'll need to replace the \uxxxx codes with their %uxxxx 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 \uxxxx-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 \uxxxx match with the coresponding character.
Helen
That's exactly what I needed ;)
tou
What does re.Replace(str, "%$1") do? What's the meaning of "%$1"?
tou
@Carlos: This code performs a replacement operation on the `str` string using a regular expression (`re`). It replaces all occurences of the `u[a-f\d]{4}` pattern (that is, *uxxxx*) preceded by \ with the same text preceded by %. `$1` in the replacement string is a shorthand for this reused pattern.
Helen
A: 

Hi,

%u559C%u529B
The above string is output of javascript escape function. I would like to unescape this using vbscript unescape, but it didn't work for me, can you suggest how i can achive this.

dfine