Extracting everything after penultimate backslash

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

    #1

    Extracting everything after penultimate backslash

    Hi all,

    What expression/function can I use to use extract a file's name and its directory,
    i.e. how to get:-

    'subdir1/file1.html' from /dir/subdir1/file1.html
    or
    'f/page.htm' from /dir1/dir2/dir3/f/page.htm

    Regards, rl


  • Martin Honnen

    #2
    Re: Extracting everything after penultimate backslash



    rl wrote:[color=blue]
    > Hi all,
    >
    > What expression/function can I use to use extract a file's name and its directory,
    > i.e. how to get:-
    >
    > 'subdir1/file1.html' from /dir/subdir1/file1.html
    > or
    > 'f/page.htm' from /dir1/dir2/dir3/f/page.htm
    >[/color]

    var strings = [
    "/dir/subdir1/file1.html",
    "/dir1/dir2/dir3/f/page.htm"
    ];
    var pattern = /[^\/]*\/[^\/]+$/;
    for (var i = 0; i < strings.length; i++) {
    var match = pattern.exec(st rings[i]);
    if (match) {
    alert(match[0]);
    }
    }[color=blue]
    >[/color]

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • rl

      #3
      Re: Extracting everything after penultimate backslash

      "Martin Honnen" <Martin.Honnen@ t-online.de> wrote in message news:3f5468b4@o laf.komtel.net. ..

      Thanks Martin,

      It works like a charm, here's what I've done with it:-

      =============== =============== =============== =============== ===
      var strings = [
      self.location.p athname
      ];

      var pattern = /[^\/]*\/[^\/]+$/;
      for (var i = 0; i < strings.length; i++) {
      var match = pattern.exec(st rings[i]);
      if (match) break;
      }

      var newURL = self.location.p rotocol +
      '//' + self.location.h ost +
      self.location.p athname.substri ng(0,self.locat ion.pathname.la stIndexOf('/')) +
      '/../index.html?cont ents.html&' + match[0];
      =============== =============== =============== =============== ===

      Regards, rl


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Extracting everything after penultimate backslash

        "rl" <sdsf@dgfedhgeh dfh.com> writes:
        [color=blue]
        > What expression/function can I use to use extract a file's name and its directory,
        > i.e. how to get:-
        >
        > 'subdir1/file1.html' from /dir/subdir1/file1.html
        > or
        > 'f/page.htm' from /dir1/dir2/dir3/f/page.htm[/color]

        function getDirectoryAnd FileName(path) {
        var lastSlash = path.lastIndexO f("/");
        var nextToLastSlash = path.lastIndexO f("/",lastSlash-1);
        return path.substring( nextToLastSlash +1);
        }

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        Working...