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 5 of 5
  1. #1
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    882
    Thanks
    53
    Thanked 6 Times in 5 Posts

    wiping a string of a certain letter/digit

    I know that there are various ways of doing this, but which is the most efficient?

    Here is a string:

    Code:
    zzzzzzzzzR210704201zzzzzzzzzzz421564214zzzzzzzzz1232233335433zz135zzzzzzzzz31359z015zzzz
    How to best delete all the instances of "z"?

    I have tried splitting the various parts of the string each side of an instance of "z" and concatenating, but I'm curious to know of a more elegant and professional solution.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Code:
    alert('zzzR210704201zzzzzzzzzzz421564214zzzzzzzzz1232233335433zz135zzzzzzzzz31359z015zzzz'.split('z').join(''))
    alert('zzzR210704201zzzzzzzzzzz421564214zzzzzzzzz1232233335433zz135zzzzzzzzz31359z015zzzz'.replace(/z/g,''))
    Last edited by DaveyErwin; 08-04-2014 at 03:10 PM.

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    var zstring='zzzzzzzzzR210704201zzzzzzzzzzz421564214zzzzzzzzz1232233335433zz135zzzzzzzzz31359z015zzzz';

    replace any consecutive z's at once-

    alert(zstring.replace(/z+/g,''));

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,695
    Thanks
    80
    Thanked 4,511 Times in 4,475 Posts
    mrhoo is pointing out that you might get better performance with his answer. I think he might be right.

    NOTE: If you wanted to retain single z's while wiping out repeated ones, you could do:
    Code:
    var zs = "zzzzzxyz keep xyz zzzz";
    var zap = zs.replace(/z{2,}/g, "");
    document.write(zap); // will write "xyz keep xyz "
    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.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,170
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    Quote Originally Posted by Old Pedant View Post
    mrhoo is pointing out that you might get better performance with his answer. I think he might be right.
    Mrhoo may well be right but I very much doubt if any there is any measureable difference.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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