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
    Join Date
    Jun 2002
    Location
    Northern NJ
    Posts
    404
    Thanks
    0
    Thanked 1 Time in 1 Post

    help with reg exp

    I have a function that should replace the specified characters with an empty string, but it doesn't work. Here is the function
    Code:
        function removeSpecials( strValue )
        {  var objRegExp = (/(" ")|("/")|("%")|("*")|("'")|(""")/)
           //replace all matches with empty strings
           return strValue.replace(objRegExp,'');
        }
    the error is an Expected ')'. All help is appreciated, thanks.
    I would rather be a lion for a day than a lamb that lives forever.

  • #2
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function removeSpecials ( strValue ) {
       //replace all matches with empty strings
       return strValue.replace(/\s\/%\*'"/g,'');
    }
    Try that. The problem was you weren't actually making a regular expression, and you were defining the replacement expression with strings. This now reads, "give me the value I passed in, with the following replaced by nothing: whitespace, slash, percent sign, asterisk, apostrophe, or quote -- and look at the whole thing before you quit, to make sure you've replaced them all."

    If you're only looking for alphanumeric characters, use this instead:
    return strValue.replace(/\W/g, '');
    Any non-word character will be replaced.

    Be sure to check the FAQ in this forum to find liorean's RegEx tutorials. Read them and love them.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Northern NJ
    Posts
    404
    Thanks
    0
    Thanked 1 Time in 1 Post
    thanks for your help and I will read the tut shortly. I've tried the suggested function above and in my example it did not work. Here is my example

    Code:
    <html>
    <head>
    <script language="javascript">
    function removeSpecials(strValue) {
       //replace all matches with empty strings
       return strValue.replace(/\s\/%\*'"/g,'');
    }
    
    function alertme()
    {
      var val;
      val = removeSpecials(document.form1.example.value)
      alert(val);
    
    }
    </script>
    </head>
    <form name=form1>
    <table>
    <tr><td><input type=text name="example"><input type=button onclick="alertme()" value="check"></td></tr>
    </table>
    </form>
    </html>
    if I am using it wrong please point that out. Could there be a problem with which javascript version I am calling? All help is appreciated, thanks.
    Last edited by ecnarongi; 05-04-2004 at 05:32 PM.
    I would rather be a lion for a day than a lamb that lives forever.

  • #4
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry about that - I forgot to group the characters. Use this in place of what it resembles:

    return strValue.replace(/[\s\/%\*'"]/g,'');

    Notice that there are now brackets surrounding all those characters, which means "find any of these."


    Edit: also, if you're not specifically limiting those characters and may care about semicolons, ampersands, and other non-word characters, I just tested with the expression /\W/g and it works just the same -- it happens to strip out all non-alphanumerics.
    Last edited by Choopernickel; 05-04-2004 at 06:44 PM.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Northern NJ
    Posts
    404
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks a lot, I need to get tight with my reg expressions.
    I would rather be a lion for a day than a lamb that lives forever.


  •  

    Posting Permissions

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