SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: string replace

  1. #1
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    string replace

    Hi,

    how do i replace ' string, when i try it i get illegal character error message..

    document.write(str.replace(',""));

    i have also tried,
    document.write(str.replace(\',""));
    document.write(str.replace(\\',""));

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Both string arguments should be enclosed in quotes.

    document.write(str.replace("'", ""));

  3. #3
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first argument to String.replace() has to be either a string or a regular expression. \' and \\' are not proper string declarations because there's no opening quote. Try this:

    Code:
    document.write(str.replace('\'',""));
    
    // or
    
    document.write(str.replace("'",""));

  4. #4
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,880
    Mentioned
    48 Post(s)
    Tagged
    0 Thread(s)
    I would try this:

    document.write(str.replace(/'/g,""));

    Makes it global, so that it will replace all occurrences of the char and not just the 1st instance. Just not sure if you will need to escape the ' or not. Give it a shot and see if it works.

    Hope this helps

  5. #5
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks it worked.


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
  •