SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict
    Join Date
    Jan 2005
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    removing special characters

    how do i remove all the illegal characters with regular expression in javascript? how should the regex look like? i am not very good with regex.

    i am trying to do like str.replace('\([\w][\d])*\g', '');
    i was thinking if i could do a condition in regex where i could say if the character is not alphanumeric, i will replace it with an empty char. but i have no idea how to do that.

    thanks.

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is there a reason for this? Could you not HTMLEncode the string? This will convert all "illegal" chars into html entities (& for &)


  3. #3
    SitePoint Addict
    Join Date
    Jan 2005
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i am trying to have the string which may contains illegal characters into the url. i am not encoding the characters into html entities because i am worry it may affect the SEO and make the url looks long and ugly as well.

    the other issue is that i have another input box where i do not allow users to enter special characters. all special characters would want to be automatically stripped off on submit.

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm,

    Code:
            function strip() {
                var string = "123*(&(*&%123897123908jkhasdklfjhsadflkjh*(&@~:{}";
                var rgx = new RegExp("[^0-9A-Za-z]","g");
                var newstring = string.replace(rgx,"*");
                alert(newstring + '\n' + string);
            }


  5. #5
    SitePoint Addict
    Join Date
    Jan 2005
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cool....just a question...
    i have the regex to look for all numbers and letters. why isn't it replacing the letters and numbers but the special characters? how did it do the opposite?

  6. #6
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes remove the ^ from the regex string. But why would you want to replace alphanumeric characters?


  7. #7
    SitePoint Addict
    Join Date
    Jan 2005
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh...no...i mean why through the way you have done, where put the regex to look for numbers and letters could end up removing the special characters?
    it looks really clever but i thought it looks more like replacing the numebers and letters instead of the opposite.

    did u mean the ^ is the character that make the regex to do the opposite?

  8. #8
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the regex string [a-zA-Z0-9] when used with a replace, will replace any alphanumeric character. Using ^ tells it to replace everything other than alphanumeric characters.


  9. #9
    SitePoint Addict
    Join Date
    Jan 2005
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but isn't ^ represents a start of a regular expression?

  10. #10
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nope. Read up on regular expressions. Different platforms deal with them in different ways.



Bookmarks

Posting Permissions

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