SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Farmington
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with \\ and \

    Hi,

    I have the following code (into a variable say, responseText)

    document.updateAccessGroup.dataLocUpd.value='\\crwd0004\grpdata'

    When I execute the above code using
    evaluate(responseText)

    I get the output as \crwd0004grpdata (stripped away one slash in the beginning and the remaining slashes)

    Also, If I have \\crwd0004\grpdata\ (notice the extra slash at the end), I get Unterminated String Constant error.

    Please help.

    Thanks
    vmrao

  2. #2
    code ninja hamstu's Avatar
    Join Date
    Nov 2002
    Location
    Montréal, Canada
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In JavaScript, the slash '\' character is known as an escape character. Alone it means nothing. But it's very useful when you combine it with another character. For example, consider the following string:
    Code:
    var mystring = "I like to use "quotes" in my strings.";
    It looks innocent enough, but it throws an error when you try to use it. The reason is that JavaScript sees the second " quote as the end of the string, and the rest of the text as invalid code.

    The problem is easily fixed by escaping the quote:
    Code:
    var mystring = "I like to use \"quotes\" in my strings.";
    Now the string works, and appears as expected.

    To show a literal backslash (as in your problem), you'll need to do it twice. So for your string:
    Code:
    document.updateAccessGroup.dataLocUpd.value='\\\\crwd0004\\grpdata'

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Farmington
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your detailed reply.

    The problem is the string "\\crwd0004\grpdata" is coming from a dynamic variable.

    I tried responseText.replace("\","\\") and I get a "Invalid Character" error.

    What should be the replace statement ?

    Thanks
    vmrao

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    responseText.replace("\\", "\\\\")
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Farmington
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried that and it is replacing only the first occurence. How do I rewrite the replace function to do it for all occurences of '\'.

    Also, I see that first occurence of "\\" got modified to "\\\" after applying the replace function. But, the output still has only one "\" instead of "\\" after the evaluate method.

    Thanks
    vmrao

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    responseText.replace(/\\/g, "\\\\")
    Birnam wood is come to Dunsinane

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Farmington
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank You very much. That worked.

    vmrao


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
  •