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
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Replacing backslashes

    Hey, i've hit a bit of a snag while using the replace function. Basically what i need to do is replace all instances of the string: \"
    with the string: \\"
    The code i'm using at the moment is as follows:

    Code:
    URL = URL.replace("\"", "\\\"")
    this works, but only replaces the first instance, and when i try to add the g-flag it stops working. I'm really stumped on this one, any ideas?

    Thanks in advance

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,990
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    URL = 'xxxxx\\"' // backslash must be escaped
    alert (URL);
    URL = URL.replace(/\\"/,'\\\\"')
    alert (URL);

    Somewhat wearily, I would like to mention that the gruesome expression "Thanks in advance" so beloved by newcomers is impolite, and takes the assistance given for granted. Most people are taught "Please" before, "Thanks" afterwards.

  • #3
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey, thank you for your quick response. I apologise for my comment, i never thought of it that way before and i certainly didn't intend it that way. With regards to the code you posted, it wasn't quite what i meant.
    The code you posted replaces the first instance of: \\"
    with: \\\"
    Also, for the URL i can't add another backslash to: \"
    unless there is a function in javascript to do so. I don't think i was that far off the answer, i just need the code i posted to work globally on all instances of the string rather than just the first occurrence.

  • #4
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just cracked it, cheers for your help though

    Code:
    originalURL = originalURL.replace(/\"/g, '\\\"')

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,990
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Yes, that's it. Sorry I misunderstood what you were trying to do.


  •  

    Posting Permissions

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