Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript replace() double characters

    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?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    It works for me. Or are there multiple instances of zz in the string? If so you need the /g (global replace) switch.

    Code:
    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.

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    New Coder
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by robbrandt View Post
    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?

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

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by robbrandt View Post
    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.

    Quote Originally Posted by Toxhicide View Post
    Im not sure if this would work, but have you tried this?

    Code:
    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.

  • #6
    New Coder
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    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.
    Last edited by Toxhicide; 12-18-2010 at 07:49 PM.

  • #7
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Toxhicide View Post
    Im not sure if this would work, but have you tried this?

    Code:
    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
    Code:
    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.

  • #8
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,436
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Well, how about this:
    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Well, how about this:
    Code:
    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:

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

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,436
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Whoops...yes, of course. SORRY! Fumble fingers.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •