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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    replace function escaped characters.

    Hello,
    I am trying to remove single quotes and double quotes from a string of escaped characters,
    it works with all except for single quotes ' and double quotes "
    this is my code:

    var newstrchanged = strEscaped.replace(/^%22/g, ' '); //does not remove the double quotes

    var newstrchanged = strEscaped.replace(/^%27/g, ' '); //does not remove the single quote

    var newstrchanged = strEscaped.replace(/^%3A/g, ' '); //does remove the SEMI COLON %3A


    thank you for your help

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    <script type = "text/javascript">
    
    var str = "abc\;def\'ghj"
    var newstr = str.replace(/[;'""]/g,"");
    alert (newstr);
    
    </script>
    It wasn't funny but it did make me laugh. - Reporter, Surrey Advertiser.

    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.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,195
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Not to ask a silly question, but...

    Why not first *UNESCAPE* the text? Which is what, I think, Philip was assuming you would do. And then do the replacement on the unescaped text.

    But anyway, your use of /^ means that it will ONLY find characters at the START of the text. That is, it will only replace the characters you are specifying if they are the first character in the string.

    So:

    Code:
    var newstrchanged = strEscaped.replace(/\%(22|27|3A)/gi, ' ');
    Equivalent of Philip's code, but using the escaped versions, instead.

    You may not need the \ in front of the % but it doesn't hurt to use it to be safe.
    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
    •