Page 1 of 1

Replacing string with special characters

Posted: Sun Sep 27, 2009 7:38 am
by jeff00seattle
I want to replace a string of special characters, but I cannot seem to get it to work in JavaScript.

For example:
Replace \xEE\x80\x80 With <strong>

I have tried it several ways, but none work:

Code: Select all

 
title.replace("\xEE\x80\x80", "<strong>");
 
title.replace("/\xEE\x80\x80/", "<strong>");
 
title.replace("[\xEE\x80\x80]", "<strong>");
 
title.replace("/[\xEE\x80\x80]/", "<strong>");
Ideas?

Jeff in Seattle

Re: Replacing string with special characters

Posted: Sun Sep 27, 2009 8:00 am
by kaszu
In javascript you don't need to use quotes if you are not using RegExp:

Code: Select all

title.replace(/\xEE\x80\x80/g, "<strong>");
//or
title.replace(new RegExp("\xEE\x80\x80", "g"), "<strong>");
 
g modifier is for 'global', otherwise only first occurrence will be replaced

Re: Replacing string with special characters

Posted: Sun Sep 27, 2009 10:41 am
by jeff00seattle
Thanks for the reply, but I tried both your solutions and nothing happens.

Ideas?

Jeff in Seattle

Re: Replacing string with special characters

Posted: Sun Sep 27, 2009 1:42 pm
by kaszu
Following works for me

Code: Select all

var title = "something \xEE\x80\x80 bla bla";
alert('Before: ' + title);
alert('After: ' + title.replace(/\xEE\x80\x80/g, "<strong>"));
alerts:

Code: Select all

Before: something î bla bla
After: something <strong> bla bla

Re: Replacing string with special characters

Posted: Sun Sep 27, 2009 4:04 pm
by jeff00seattle
I am getting this data from a JSON endpoint, and I provided it with a JavaScript callback.

Since I am having some trouble with this issue, I should take a little harder look at exactly what is being returned in HEX within a Script debugger. I am expecting that the enhancement characters to be \uE000 (\xEE\x80\x80) and \uE001 (\xEE\x80\x81), and they are being ignored.

Thanks for the help!

Jeff in Seattle

Re: Replacing string with HEX characters ignored

Posted: Mon Sep 28, 2009 3:31 am
by jeff00seattle
If I used your test and it worked fine:

Code: Select all

var title = "something \xEE\x80\x80 bla bla";
alert('Before: ' + title);
alert('After: ' + title.replace(/\xEE\x80\x80/g, "<strong>"));
 
Using the the following HTTP viewer that has HEX output: http://www.rexswain.com/cgi-bin/httpview.cgi, I verified that characters (to be replaced) returned from the JSON response are HEX EE8080:

Code: Select all

      B0: 73223A5B7B225469 746C65223A22[b]EE80[/b] s":[{"Ti tle":"••
      C0: [b]80[/b]4D6F6DEE80812C 20[b]EE8080[/b]6170706C •Mom•••, ·•••appl
      D0: 65EE808120[b]EE8080[/b] 706965EE80812061 e•••·••• pie•••·a
But with this code, the characters are ignored:

Code: Select all

title.replace(/\xEE\x80\x80/g, "<strong>");
I am baffled!

Jeff in Seattle

Re: Replacing string with special characters

Posted: Mon Sep 28, 2009 10:54 am
by jeff00seattle
I think I found the problem, in the debugger (IE8), I look at the applied methods available to variable containing a string type, and it does not include: split() or replace().

Why would this occur?

Do I need to load a string library to make this functions available?

Jeff in Seattle

Re: Replacing string with special characters

Posted: Tue Sep 29, 2009 11:31 am
by jeff00seattle
Figured it out what I was doing wrong:

I was doing this:

Code: Select all

text.replace(/\uE000/g, "<strong>");
text.replace(/\uE001/g, "</strong>");
When I should have done this:

Code: Select all

text = text.replace(/\uE000/g, "<strong>");
text = text.replace(/\uE001/g, "</strong>");
Thanks for the help