replace '\' ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chuck clark

    replace '\' ?

    Hi,
    I would like to enter a folder path like this
    C:\Documents and Settings\chuckl arge\Desktop
    and get this
    C:\\Documents and Settings\\chuck large\\Desktop\ \

    this is what i have tried but can not get it right

    function preparePath(fol derPath)
    { var len = folderPath.leng th;
    if(folderPath[len - 1] != '\\')
    { folderPath += '\\';
    }
    return folderPath.repl ace(\\, '\\\');
    }

    sourceFolderPat h = preparePath("C: \Documents and
    Settings\chuckl arge\Desktop");


    Any Help is appreciated,
    Chuck

  • Brian Genisio

    #2
    Re: replace '\' ?

    chuck clark wrote:
    [color=blue]
    > Hi,
    > I would like to enter a folder path like this
    > C:\Documents and Settings\chuckl arge\Desktop
    > and get this
    > C:\\Documents and Settings\\chuck large\\Desktop\ \
    >
    > this is what i have tried but can not get it right
    >
    > function preparePath(fol derPath)
    > { var len = folderPath.leng th;
    > if(folderPath[len - 1] != '\\')
    > { folderPath += '\\';
    > }
    > return folderPath.repl ace(\\, '\\\');
    > }
    >
    > sourceFolderPat h = preparePath("C: \Documents and
    > Settings\chuckl arge\Desktop");
    >
    >
    > Any Help is appreciated,
    > Chuck
    >[/color]

    Here ya go... I think you are getting confused about escape characters.
    Whenever you have a string with a \, there needs to be another
    character, so "\\\\" evaluates to \\. In your function, you are
    replacing \\ with "\\\" which is guaranteed to break, since you are
    escaping the second double-quote, and the string never closes.

    Also, in your test, you need to add \\ in the path, because it will be
    evaluated as \ before you send it into the function. _THIS IS
    DIFFERENT_ from if you are handed a string with a \ in it. The escape
    character \ is only used by the inputting user.

    Brian

    function preparePath(fol derPath)
    {
    if(folderPath[folderPath.leng th - 1] != "\\")
    folderPath += "\\";

    return folderPath.repl ace(/\\/g, "\\\\");
    }

    sourceFolderPat h = preparePath("C: \\Documents and
    Settings\\chuck large\\Desktop" );


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: replace '\' ?

      chuck clark <chuck@no-spam.mail.utexa s.edu> writes:
      [color=blue]
      > I would like to enter a folder path like this
      > C:\Documents and Settings\chuckl arge\Desktop
      > and get this
      > C:\\Documents and Settings\\chuck large\\Desktop\ \[/color]

      folderPath = folderPath.repl ace(/\\/g,"\\\\");

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...