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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Feb 2009
    Posts
    119
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Can someone please provide me Regex to replace the following characters with space in

    Can someone please provide me Regex to replace the following characters with space in JavaScript.

    '
    [
    |
    @
    &
    \
    /
    #
    +
    (
    )
    $
    ~
    %
    :
    *
    ?
    <
    >
    {
    }
    "

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,029
    Thanks
    36
    Thanked 495 Times in 489 Posts

    Lightbulb

    Don't really need to use a regex if you don't want to...
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Untitled </title>
    <script type="text/javascript">
    var ckChars = "a'b[c|d@e&f\g/h#i+j(k)l$m~n%o:p*q?r<s>t{u}v"+'w"x';
    
    String.prototype.removeBadChars = function() {
      var badChars = "'[|@&\/#+()$~%:*?<>{}"+'"';
      var str = '';
      for (var i=0; i<this.length; i++) {
        if (badChars.indexOf(this[i]) == -1) { str += this[i]; }
      }
      return str;
    }
    
    alert(ckChars.removeBadChars());
    </script>
    
    </head>
    <body>
    
    </body>
    </html>

  • #3
    Regular Coder
    Join Date
    Feb 2009
    Posts
    119
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jmrker View Post
    Don't really need to use a regex if you don't want to...
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Untitled </title>
    <script type="text/javascript">
    var ckChars = "a'b[c|d@e&f\g/h#i+j(k)l$m~n%o:p*q?r<s>t{u}v"+'w"x';
    
    String.prototype.removeBadChars = function() {
      var badChars = "'[|@&\/#+()$~%:*?<>{}"+'"';
      var str = '';
      for (var i=0; i<this.length; i++) {
        if (badChars.indexOf(this[i]) == -1) { str += this[i]; }
      }
      return str;
    }
    
    alert(ckChars.removeBadChars());
    </script>
    
    </head>
    <body>
    
    </body>
    </html>
    I am using the follwoing code but it is not working:
    AssessorComment = AssessorComment.replace(/'[|@&\/\\#+()$~%:*?<>{}/""]/g, " ");

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,029
    Thanks
    36
    Thanked 495 Times in 489 Posts

    Lightbulb

    Quote Originally Posted by asifakhtar View Post
    I am using the follwoing code but it is not working:
    AssessorComment = AssessorComment.replace(/'[|@&\/\\#+()$~%:*?<>{}/""]/g, " ");
    You are not defining what the start value of AssessorComment is,
    but regardless of that you are not using the function correctly.

    Try:
    AssessorComment = AssessorComment.removeBadChars();

    You can rename the prototype function, if you don't like the name.


    Note also that your regex definition is invalid
    /'[|@&\/\\#+()$~%:*?<>{}/""]/g, '' "
    Last edited by jmrker; 12-28-2013 at 03:42 AM.

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,029
    Thanks
    36
    Thanked 495 Times in 489 Posts
    Just noticed that you wanted to replace with a space, not remove the bad character.

    So following change might work better:
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Untitled </title>
    <script type="text/javascript">
    var ckChars = "a'b[c|d@e&f\g/h#i+j(k)l$m~n%o:p*q?r<s>t{u}v"+'w"x';
    
    String.prototype.removeBadChars = function() {
      var badChars = "'[|@&\/#+()$~%:*?<>{}"+'"';
      var str = '';
      for (var i=0; i<this.length; i++) {
        if (badChars.indexOf(this[i]) == -1) { str += this[i]; } else { str += ' '; }
      }
      return str;
    }
    
    alert(ckChars.removeBadChars());
    </script>
    
    </head>
    <body>
    
    </body>
    </html>

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,775
    Thanks
    55
    Thanked 519 Times in 516 Posts
    but if you wanted to replace anything that wasn't a letter, number, dash or underscore I think this would work...

    Code:
    AssessorComment =AssessorComment.replace(/[^A-Za-z\d_-]+/g, ' ');

  • #7
    Regular Coder
    Join Date
    Feb 2009
    Posts
    119
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho View Post
    but if you wanted to replace anything that wasn't a letter, number, dash or underscore I think this would work...

    Code:
    AssessorComment =AssessorComment.replace(/[^A-Za-z\d_-]+/g, ' ');
    I just want to replace the characters mentioned above with space.

  • #8
    Regular Coder
    Join Date
    Feb 2009
    Posts
    119
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jmrker View Post
    Just noticed that you wanted to replace with a space, not remove the bad character.

    So following change might work better:
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Untitled </title>
    <script type="text/javascript">
    var ckChars = "a'b[c|d@e&f\g/h#i+j(k)l$m~n%o:p*q?r<s>t{u}v"+'w"x';
    
    String.prototype.removeBadChars = function() {
      var badChars = "'[|@&\/#+()$~%:*?<>{}"+'"';
      var str = '';
      for (var i=0; i<this.length; i++) {
        if (badChars.indexOf(this[i]) == -1) { str += this[i]; } else { str += ' '; }
      }
      return str;
    }
    
    alert(ckChars.removeBadChars());
    </script>
    
    </head>
    <body>
    
    </body>
    </html>
    Thank you it works but how can I fix my Regex?

  • #9
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,775
    Thanks
    55
    Thanked 519 Times in 516 Posts
    maybe
    Code:
    AssessorComment =AssessorComment.replace(/['|@&\/\\#+\[\]()$~%:*?<>{}"]+/g, ' ');
    ?

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by xelawho View Post
    maybe
    Code:
    AssessorComment =AssessorComment.replace(/['|@&\/\\#+\[\]()$~%:*?<>{}"]+/g, ' ');
    ?
    Yes, that's right.

    It is worth noting that the only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (\), the caret (^) and the hyphen (-).

    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
    •