String-functions?

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

    #1

    String-functions?

    Hi,

    if I got a String like: http://server/dir1/dir2/dir3/
    how would i extract everything between '//' and the most left single '/'?
    Thank you!
    --
    roN



  • Justin Koivisto

    #2
    Re: String-functions?

    Ron Eggler wrote:
    [color=blue]
    > Hi,
    >
    > if I got a String like: http://server/dir1/dir2/dir3/
    > how would i extract everything between '//' and the most left single '/'?
    > Thank you![/color]

    var s = 'http://server/dir1/dir2/dir3/';
    s = s.match(/\/\/([^\/]*)\//);
    s = s[1];
    alert(s);

    --
    Justin Koivisto - justin@koivi.co m

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: String-functions?

      "Ron Eggler" <NOspam@hotmail .com> writes:
      [color=blue]
      > if I got a String like: http://server/dir1/dir2/dir3/
      > how would i extract everything between '//' and the most left single '/'?[/color]

      Sounds like a job for regular expressions:

      function extractDomain(u rl) {
      var match = /\/\/([^\/]*)\//.exec(url); // regexp //([^/])*/
      if (match) { return match[1]; }
      }

      /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

      • Grant Wagner

        #4
        Re: String-functions?



        --
        Grant Wagner <gwagner@agrico reunited.com>
        comp.lang.javas cript FAQ - http://jibbering.com/faq
        "Ron Eggler" <NOspam@hotmail .com> wrote in message
        news:3f1la1F5gt 45U1@individual .net...[color=blue]
        > Hi,
        >
        > if I got a String like: http://server/dir1/dir2/dir3/
        > how would i extract everything between '//' and the most left single
        > '/'?[/color]

        <script type="text/javascript">
        var s = 'http://server/dir1/dir2/dir3/';
        var start = s.indexOf('//');
        var end = s.lastIndexOf('/');
        alert(s.substri ng(start + 2, end));
        </script>

        This will fail in most interesting ways if there are no slashes in the
        string (or only two - http://).

        You can also use regular expressions to extract the information you
        seek.

        And if the url is actually in the location object, then you can extract
        the information using location.host and location.pathna me <url:
        http://docs.sun.com/source/816-6408-10/location.htm />.

        --
        Grant Wagner <gwagner@agrico reunited.com>
        comp.lang.javas cript FAQ - http://jibbering.com/faq


        Comment

        • Evertjan.

          #5
          Re: String-functions?

          Lasse Reichstein Nielsen wrote on 18 mei 2005 in comp.lang.javas cript:
          [color=blue]
          > "Ron Eggler" <NOspam@hotmail .com> writes:
          >[color=green]
          >> if I got a String like: http://server/dir1/dir2/dir3/
          >> how would i extract everything between '//' and the most left single
          >> '/'?[/color]
          >
          > Sounds like a job for regular expressions:
          >
          > function extractDomain(u rl) {
          > var match = /\/\/([^\/]*)\//.exec(url); // regexp //([^/])*/
          > if (match) { return match[1]; }[/color]

          return '';
          [color=blue]
          > }[/color]

          you would want a string returned if there is no match.

          =============== ==

          or:

          function extractDomain(u rl) {
          return /^http:\/\/([^\/]*)\/.*$/i.replace(url,' $1')
          }



          --
          Evertjan.
          The Netherlands.
          (Replace all crosses with dots in my emailaddress)

          Comment

          • Tomasz Cenian

            #6
            Re: String-functions?

            Ron Eggler napisa³(a):[color=blue]
            > Hi,
            >
            > if I got a String like: http://server/dir1/dir2/dir3/
            > how would i extract everything between '//' and the most left single '/'?
            > Thank you![/color]

            You absolutely dont need (and shouldnt use) regular expressions.


            $d = $string.split('/')[2]

            will do the trick.

            --
            zaj gezunt!
            tomasz cenian tcenian at wa dot home dot pl
            :::: :: : : http://cenian.boo.pl : : :: ::::

            Comment

            • Kristian Thy

              #7
              Re: String-functions?

              Quoth Tomasz Cenian:[color=blue]
              > Ron Eggler napisa³(a):[color=green]
              >> Hi,
              >>
              >> if I got a String like: http://server/dir1/dir2/dir3/
              >> how would i extract everything between '//' and the most left single '/'?
              >> Thank you![/color]
              >
              > You absolutely dont need (and shouldnt use) regular expressions.[/color]
              ^^^^^^^^^^^^^^^ ^

              I agree on the "don't need", but what's with the "shouldn't use"?

              --
              \\kristian

              Comment

              • Tomasz Cenian

                #8
                Re: String-functions?

                Kristian Thy napisał(a):[color=blue][color=green]
                >>
                >>You absolutely dont need (and shouldnt use) regular expressions.[/color]
                >
                > ^^^^^^^^^^^^^^^ ^
                >
                > I agree on the "don't need", but what's with the "shouldn't use"?
                >[/color]

                regexps are huge and very powerful tools. I believe that if the use of
                them in a particular case is not necessary (like this one) they should
                not be used. This is a general programming rule. You should always use
                the most efficient and straightforward method to achieve the desired thing.

                --
                zaj gezunt!
                tomasz cenian tcenian at wa dot home dot pl
                :::: :: : : http://cenian.boo.pl : : :: ::::

                Comment

                • Kristian Thy

                  #9
                  Re: String-functions?

                  Quoth Tomasz Cenian:[color=blue]
                  > regexps are huge and very powerful tools. I believe that if the use of
                  > them in a particular case is not necessary (like this one) they should
                  > not be used. This is a general programming rule. You should always use
                  > the most efficient and straightforward method to achieve the desired thing.[/color]

                  Ah, okay :)

                  I thought you meant you shouldn't use regexps generally.

                  --
                  \\kristian

                  Comment

                  • Dr John Stockton

                    #10
                    Re: String-functions?

                    JRS: In article <3f1la1F5gt45U1 @individual.net >, dated Wed, 18 May 2005
                    22:02:08, seen in news:comp.lang. javascript, Ron Eggler
                    <NOspam@hotmail .com> posted :
                    [color=blue]
                    >if I got a String like: http://server/dir1/dir2/dir3/
                    >how would i extract everything between '//' and the most left single '/'?[/color]

                    If you are sure that there are no slashes before the //, then

                    S = S.split('/')[2]

                    will do it, except if there are browsers that won't return an empty
                    string for S[1] - in which case consider

                    S = S.split(/\/+/)[1]

                    which assumes that the field you want is not empty.

                    --
                    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                    <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                    <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                    <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                    Comment

                    • Lasse Reichstein Nielsen

                      #11
                      Re: String-functions?

                      Tomasz Cenian <aaa@bb.cc.dd > writes:
                      [color=blue]
                      > You absolutely dont need (and shouldnt use) regular expressions.
                      >
                      > $d = $string.split('/')[2]
                      >
                      > will do the trick.[/color]

                      Only if the string has the correct format. There is no check for this.

                      It will still do too much work, since creating the other substrings is
                      not necessary.

                      var i1 = string.indexOf( "//") + 2;
                      var i2 = string.indexOf( "/", i1);
                      return string.substrin g(i1,i2);

                      /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

                      • RobG

                        #12
                        Re: String-functions?

                        Lasse Reichstein Nielsen wrote:[color=blue]
                        > "Ron Eggler" <NOspam@hotmail .com> writes:
                        >
                        >[color=green]
                        >>if I got a String like: http://server/dir1/dir2/dir3/
                        >>how would i extract everything between '//' and the most left single '/'?[/color]
                        >
                        >
                        > Sounds like a job for regular expressions:
                        >
                        > function extractDomain(u rl) {
                        > var match = /\/\/([^\/]*)\//.exec(url); // regexp //([^/])*/
                        > if (match) { return match[1]; }
                        > }[/color]

                        Cool, but allowing for Mozilla-style - file:///usr/...
                        and IE-style - file://c:\...
                        and putting in the colon ':' for added safety, how about:

                        var match = /:\/{2,3}([^\/|\\]*)(\/|\\)/.exec(url);



                        --
                        Rob

                        Comment

                        • Lasse Reichstein Nielsen

                          #13
                          Re: String-functions?

                          RobG <rgqld@iinet.ne t.auau> writes:
                          [color=blue]
                          > Cool, but allowing for Mozilla-style - file:///usr/...[/color]

                          That's not just Mozilla-style. It's the official shorthand for
                          file://localhost/usr/...

                          /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

                          • Mick White

                            #14
                            Re: String-functions?

                            Ron Eggler wrote:
                            [color=blue]
                            > Hi,
                            >
                            > if I got a String like: http://server/dir1/dir2/dir3/
                            > how would i extract everything between '//' and the most left single '/'?
                            > Thank you![/color]

                            You're looking for "server"?

                            Str=Str.split("//")[1].split("/")[0];

                            But if this a location object, there are several built-in properties.
                            Mick

                            Comment

                            • Thomas 'PointedEars' Lahn

                              #15
                              Re: String-functions?

                              Tomasz Cenian wrote:
                              [color=blue]
                              > Kristian Thy napisa?(a):[color=green][color=darkred]
                              >>>You absolutely dont need (and shouldnt use) regular expressions.[/color]
                              >> ^^^^^^^^^^^^^^^ ^
                              >> I agree on the "don't need", but what's with the "shouldn't use"?[/color]
                              >
                              > regexps are huge and very powerful tools. I believe that if the use of
                              > them in a particular case is not necessary (like this one) they should
                              > not be used. This is a general programming rule. You should always use
                              > the most efficient and straightforward method to achieve the desired
                              > thing.[/color]

                              You seldom know the length of the string to be parsed and so regular
                              expressions are in most cases the most efficient and straightforward
                              method.


                              PointedEars

                              Comment

                              Working...