Reg exp to replace \ with /

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

    #1

    Reg exp to replace \ with /

    Hi,
    I'm not familiar with JavaScript but have to fix a problem.
    Please see below part of the HTML code with contains my question:

    The problem is that I have to display a path in the showIt function.
    If I write the path (C:\TEMP\396.x0 0) directly in the document.write( )
    function is shows the path without the backslashed.

    So I though I create a fixPath function that replaces the single backslash
    to a forward backslash (better a forward slash than no slash; but I would
    like to see a backslash if possible)

    The fixPath however doesn't replace the backslash with a forward slash?
    Why not?

    FYI: I can't change the input string as it is generated by another program

    Please help me.
    Frank

    ....

    <script language="JavaS cript" type="text/javascript">
    <!--


    function fixPath( s )
    {
    return s.replace(/\\/g, "/");
    }


    function showIt()
    {
    document.write( fixPath("C:\TEM P\396.x00")+"<b r>");
    }
    ....
  • McKirahan

    #2
    Re: Reg exp to replace \ with /

    "Frank" <fbeo@hotmail.c om> wrote in message
    news:50eb7d3d.0 407220402.7756a 0ed@posting.goo gle.com...[color=blue]
    > Hi,
    > I'm not familiar with JavaScript but have to fix a problem.
    > Please see below part of the HTML code with contains my question:
    >
    > The problem is that I have to display a path in the showIt function.
    > If I write the path (C:\TEMP\396.x0 0) directly in the document.write( )
    > function is shows the path without the backslashed.
    >
    > So I though I create a fixPath function that replaces the single backslash
    > to a forward backslash (better a forward slash than no slash; but I would
    > like to see a backslash if possible)
    >
    > The fixPath however doesn't replace the backslash with a forward slash?
    > Why not?
    >
    > FYI: I can't change the input string as it is generated by another program
    >
    > Please help me.
    > Frank
    >
    > ...
    >
    > <script language="JavaS cript" type="text/javascript">
    > <!--
    >
    >
    > function fixPath( s )
    > {
    > return s.replace(/\\/g, "/");
    > }
    >
    >
    > function showIt()
    > {
    > document.write( fixPath("C:\TEM P\396.x00")+"<b r>");
    > }
    > ...[/color]


    Double up your backslashes:

    "C:\\TEMP\\396. x00"


    Comment

    • Frank

      #3
      Re: Reg exp to replace \ with /

      "McKirahan" <News@McKirahan .com> wrote in message news:<gOOLc.161 107$Oq2.51416@a ttbi_s52>...[color=blue]
      > "Frank" <fbeo@hotmail.c om> wrote in message
      > news:50eb7d3d.0 407220402.7756a 0ed@posting.goo gle.com...[color=green]
      > > Hi,
      > > I'm not familiar with JavaScript but have to fix a problem.
      > > Please see below part of the HTML code with contains my question:
      > >
      > > The problem is that I have to display a path in the showIt function.
      > > If I write the path (C:\TEMP\396.x0 0) directly in the document.write( )
      > > function is shows the path without the backslashed.
      > >
      > > So I though I create a fixPath function that replaces the single backslash
      > > to a forward backslash (better a forward slash than no slash; but I would
      > > like to see a backslash if possible)
      > >
      > > The fixPath however doesn't replace the backslash with a forward slash?
      > > Why not?
      > >
      > > FYI: I can't change the input string as it is generated by another program
      > >
      > > Please help me.
      > > Frank
      > >
      > > ...
      > >
      > > <script language="JavaS cript" type="text/javascript">
      > > <!--
      > >
      > >
      > > function fixPath( s )
      > > {
      > > return s.replace(/\\/g, "/");
      > > }
      > >
      > >
      > > function showIt()
      > > {
      > > document.write( fixPath("C:\TEM P\396.x00")+"<b r>");
      > > }
      > > ...[/color]
      >
      >
      > Double up your backslashes:
      >
      > "C:\\TEMP\\396. x00"[/color]


      Hi,
      Thanks for the answer, but as I indicated I can't change the string
      itself as it is generated by another program. I browsed this group a
      bit and I think I must conclude that I can't fix my problem without
      having a string that contains the double slash....Men, I'm glad my
      daily job is not programming JavaScript ......;-)

      Frank

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Reg exp to replace \ with /

        fbeo@hotmail.co m (Frank) writes:
        [color=blue]
        > Thanks for the answer, but as I indicated I can't change the string
        > itself as it is generated by another program.[/color]

        Yes, then you are lost. The other program generates Javascript with
        less information than what you need, and there is no way around that ...
        except, perhaps, if you are *really* desperate.

        If you know that this string is contained inside a script on the page,
        then you can (in some browsers) access the literal script content.
        Say it's in the third script element on the page, then some browsers
        (IE 5+, Mozilla, Opera 7) allows you to see the content of that
        script element as:

        document.getEle mentsByTagName( "script")[2].text

        If the string is somehow recognizable (starting with "C:" is suffient)
        then you can find it inside the text:

        var scriptText = document.getEle mentsByTagName( "script")[2].text;
        var start = scriptText.inde xOf('"C:') + 1;
        var end = scriptText.inde xOf('"', start);
        var literalText = scriptText.subs tring(start,end );


        Example page:
        ---
        <script type="text/javascript">
        var stupidString = "C:\w\y\z"; // by someone else
        </script>
        <script type="text/javascript">
        var scriptText = document.getEle mentsByTagName( "script")[0].text;
        var start = scriptText.inde xOf('"C:') + 1;
        var end = scriptText.inde xOf('"', start);
        var literalText = scriptText.subs tring(start,end );
        alert(literalTe xt);
        </script>
        ---

        On the other hand, if the script is included from an external file
        (which it probably is, since you don't have control over it), then
        I don't think there is anything to do. Hmm, and again. If you know
        the user is using IE or Mozilla, then you might fetch the page again
        using some sort of XMLHTTPRequest (but that's out of my scope, I
        use neither IE nor Mozilla :).
        [color=blue]
        > I browsed this group a bit and I think I must conclude that I can't
        > fix my problem without having a string that contains the double
        > slash....[/color]

        That would be the obvious solution, yes.
        And just wait until you hit a directory starting with "x", "u" or "0"
        (which start multi-character escape sequences that, if malformed
        will be syntactic errors in your page, e.g.: "C:\x_drive\use r" :)
        [color=blue]
        > Men, I'm glad my daily job is not programming JavaScript
        > ......;-)[/color]

        If it were, you'd most likely be in a position to force whoever
        provided the string to stop being stupid :)

        /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

        • Thomas 'PointedEars' Lahn

          #5
          Re: Reg exp to replace \ with /

          Lasse Reichstein Nielsen wrote:[color=blue]
          > fbeo@hotmail.co m (Frank) writes:[color=green]
          >> Thanks for the answer, but as I indicated I can't change the string
          >> itself as it is generated by another program.[/color]
          >
          > Yes, then you are lost. [...][/color]

          IMHO s/are/have/ (CMIIW).


          PointedEars

          Comment

          Working...