Total regular expressions virgin - string help!

Discussion in 'Javascript' started by Tony, Jul 30, 2005.

  1. Tony

    Tony Guest

    Via regular expressions, how do I replace characters in a string with
    ""(nothing) if they aren't in an array of allowed characters?

    var allowed = ["x","y"," "];

    var string = "xyz w yz"; - replace everything not in allowed array.
     
    Tony, Jul 30, 2005
    #1
    1. Advertising

  2. Tony

    RobG Guest

    Tony wrote:
    > Via regular expressions, how do I replace characters in a string with
    > ""(nothing) if they aren't in an array of allowed characters?
    >
    > var allowed = ["x","y"," "];
    >
    > var string = "xyz w yz"; - replace everything not in allowed array.
    >


    <input type="text" value="zxujh yjhg xzhgt" onblur="

    var allowed = [ 'x', 'y', ' '];
    var re = new RegExp( '[^' + allowed.join('') + ']', 'g' );

    alert( this.value.replace( re, '') );

    ">




    --
    Rob
     
    RobG, Jul 30, 2005
    #2
    1. Advertising

  3. Tony

    Guest

    > What RobG wrote:



    Excellent, thanks Rob.
     
    , Jul 30, 2005
    #3
  4. Tony

    Guest

    > What RobG wrote:



    Excellent, thanks Rob.
     
    , Jul 30, 2005
    #4
  5. Tony

    RobG Guest

    wrote:
    >>What RobG wrote:

    >
    >
    > Excellent, thanks Rob.
    >


    The use of an array is not necessary, the allowed characters can be a
    simple string:

    var allowed = 'xy ';
    var re = new RegExp( '[^' + allowed + ']', 'g' );

    --
    Rob
     
    RobG, Jul 31, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas
    Replies:
    0
    Views:
    610
    Jay Douglas
    Aug 15, 2003
  2. chad
    Replies:
    4
    Views:
    3,829
  3. google 24/7
    Replies:
    20
    Views:
    771
    Colin Hemmings
    Feb 20, 2006
  4. RichardOnRails
    Replies:
    1
    Views:
    99
    Brian Candler
    Oct 28, 2008
  5. Noman Shapiro
    Replies:
    0
    Views:
    235
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page