...

View Full Version : javascript replace() double characters



robbrandt
12-18-2010, 04:45 AM
This is driving me nuts :)

I need to replace all instances of "zz" with 'Z'. I can easily replace other strings such as

a = a.replace(/mickeymouse/, "Z");

but

a = a.replace(/zz/, "Z");

doesn't work. Must be a double characer thing?

Philip M
12-18-2010, 08:56 AM
It works for me. :) Or are there multiple instances of zz in the string? If so you need the /g (global replace) switch.


a = "abczzdefzz";
a = a.replace(/zz/g, "Z");
alert (a);


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

robbrandt
12-18-2010, 08:26 PM
Doesn't work for me. Nice to know that I am on the right track.

Any ideas why, in this instance, it doesn't work for me?

Toxhicide
12-18-2010, 08:33 PM
Doesn't work for me. Nice to know that I am on the right track.

Any ideas why, in this instance, it doesn't work for me?

Im not sure if this would work, but have you tried this?


a = a.replace("/zz/", "Z");

Philip M
12-18-2010, 08:39 PM
Doesn't work for me. Nice to know that I am on the right track.

Any ideas why, in this instance, it doesn't work for me?

No. Because as I say it works just fine for me. :)


Im not sure if this would work, but have you tried this?


a = a.replace("/zz/", "Z");

If you had tried it you would have found that it does not work - that is not the correct syntax.

Toxhicide
12-18-2010, 08:47 PM
No. Because as I say it works just fine for me. :)



If you had tried it you would have found that it does not work - that is not the correct syntax.

Ive been trying to make a test html to test it, but my computer is messing up today.

Edit: just got it working, and yes your code does work. I dont know why it isnt working for him.

robbrandt
12-18-2010, 09:28 PM
Im not sure if this would work, but have you tried this?


a = a.replace("/zz/", "Z");

Yes, I did try that, causes the page to crash. Bad syntax, as stated.

I'll try and round out a little more information.

the purpose of this is to tokenize certain characters for string manipulation, this is but the final step; to "interpret" consecutive series of tokens for a single instance of the token. Prior replace code works just fine. The string starts out as XML, something like this:

a starts out equal to
this is a string <tag></tag><tag></tag> that's about this long

I then do a replace on the tags to tokenize them, which works just fine and results in "this is a string zz that's about this long". I then do some other stuff (ultimately, but I actually have it commented out for testing to eliminate variables). The last step is to convert the zz into Z, which isn't working. I actually don't care what tokens are used, they aren't that important.

Don't know that this matters, but I am doing my coding on Aptana on OS X, and using Firefox to test run on a locally installed copy of XAMPP.

robbrandt
12-21-2010, 02:00 AM
Today I tried placing "zz" directly into the string I am manipulating, and the replace worked just fine on that. I can only conclude that previous instances that appeared to be "zz" aren't really "zz", there is probably a hidden character between them. Obvious candidates are \r and \n, which I tried in various combinations without success.

Old Pedant
12-21-2010, 02:27 AM
Well, how about this:


a = a.replace(/z[^\s\!-y]?z", "Z")

[^xxx] means any character *EXCEPT* those listed.
and using [^\s\!-y] means any character except whitespace or any character in the normal ASCII set from ! to y, which covers all the normal "printable" characters.

robbrandt
12-21-2010, 02:34 AM
Well, how about this:


a = a.replace(/z[^\s\!-y]?z", "Z")

[^xxx] means any character *EXCEPT* those listed.
and using [^\s\!-y] means any character except whitespace or any character in the normal ASCII set from ! to y, which covers all the normal "printable" characters.

Seems to be a syntax error there, did you mean:


a = a.replace(/z[^\s\!-y]?z/, "Z")

Old Pedant
12-21-2010, 03:10 AM
Whoops...yes, of course. SORRY! Fumble fingers.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum