Regular expression help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FrankIsHere@gmail.com

    Regular expression help

    How can I strip out the text from within this "href" attribute?

    Here is the input:
    <span href="/test.htm&parame ter=2"></span>

    What I'd like to get back is:
    /test.htm&parame ter=2


    Thanks!
    Frank

  • Evertjan.

    #2
    Re: Regular expression help

    wrote on 20 jan 2006 in comp.lang.javas cript:
    [color=blue]
    > How can I strip out the text from within this "href" attribute?
    >
    > Here is the input:
    > <span href="/test.htm&parame ter=2"></span>
    >
    > What I'd like to get back is:
    > /test.htm&parame ter=2[/color]

    t = '<span href="/test.htm&parame ter=2"></span>'

    t = t.replace(/^.*"(.*)".*$/,'$1')

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Regular expression help

      FrankIsHere@gma il.com writes:
      [color=blue]
      > How can I strip out the text from within this "href" attribute?
      >
      > Here is the input:
      > <span href="/test.htm&parame ter=2"></span>
      >
      > What I'd like to get back is:
      > /test.htm&parame ter=2[/color]

      Well, that's easy:

      function getHref(input) {
      return "/test.htm&parame ter=2";
      }

      But seriously, you should be very careful to know exactly what can
      vary in the input. Is it always a span element (unlikely, since
      they don't have href attributes)? Is it always the href attribute
      you need? And is it always contained in double quotes?

      Let's assume that you are looking for just one href attribute where
      the value is in double quotes. Then the following regular expression
      will capture that:

      var re = /\bhref="([^"]*)"/;

      In a function it would be:
      function getHrefRE(input ) {
      var re = /\bhref="([^"]*)"/;
      var match = input.match(re) ;
      if (match) {
      return match[1];
      }
      }


      If the quotes can be either single and double quote, a regexp might
      be:
      var re2 = /\bhref=(['"])([^\1]*)\1/;
      where the content is match[2].


      Good luck
      /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

        #4
        Re: Regular expression help

        FrankIsHere@gma il.com wrote:[color=blue]
        > How can I strip out the text from within this "href" attribute?
        >
        > Here is the input:
        > <span href="/test.htm&parame ter=2"></span>
        >
        > What I'd like to get back is:
        > /test.htm&parame ter=2
        >[/color]

        Normally you can just get the attribute value:

        alert( refToSpan.href ); // Shows /test.htm&parame ter=2


        But span elements don't have href attributes so it will not work
        reliably - some browsers do not let you access invalid attributes. You
        could use an A element:


        <a href="/test.htm&parame ter=2" name="theLink"> </a>

        <script type="text/javascript">
        alert( document.links[0].href );
        </script>



        --
        Rob

        Comment

        • mick white

          #5
          Re: Regular expression help

          FrankIsHere@gma il.com wrote:[color=blue]
          > How can I strip out the text from within this "href" attribute?
          >
          > Here is the input:
          > <span href="/test.htm&parame ter=2"></span>
          >
          > What I'd like to get back is:
          > /test.htm&parame ter=2
          >
          >[/color]
          output="/"+input.spl it("/")[1]

          Mick

          Comment

          • Evertjan.

            #6
            Re: Regular expression help

            mick white wrote on 20 jan 2006 in comp.lang.javas cript:
            [color=blue]
            > FrankIsHere@gma il.com wrote:[color=green]
            >> How can I strip out the text from within this "href" attribute?
            >>
            >> Here is the input:
            >> <span href="/test.htm&parame ter=2"></span>
            >>
            >> What I'd like to get back is:
            >> /test.htm&parame ter=2
            >>
            >>[/color]
            > output="/"+input.spl it("/")[1][/color]

            gives:

            /test.htm&parame ter=2"><

            --
            Evertjan.
            The Netherlands.
            (Please change the x'es to dots in my emailaddress)

            Comment

            • FrankIsHere@gmail.com

              #7
              Re: Regular expression help

              Thanks guys!

              The reason I use it in a span tag and not an <a> tag is because of
              custom XSL parsing I do on my web application, but that is irrelevant
              to the problem.

              I have been doing it this way and it seems to work so far, but I'm not
              a javascript guy so I don't know if there could be any potential
              problems with the way I'm doing it. What do you guys think?

              Here's what I'm using:

              function stripHref(tag) {
              test2 = tag.replace("<s pan href=","");
              last = test2.replace(" ></span>","");

              //alert(last);
              if (last.match(/^'.*'$/) || last.match(/^".*"$/)) {
              return last.substring( 1,last.length-1);
              }
              return last;
              }


              -Frank

              Comment

              • mick white

                #8
                Re: Regular expression help

                Evertjan. wrote:
                [color=blue]
                > mick white wrote on 20 jan 2006 in comp.lang.javas cript:
                >
                >[color=green]
                >>FrankIsHere@g mail.com wrote:
                >>[color=darkred]
                >>><span href="/test.htm&parame ter=2"></span>
                >>>
                >>>What I'd like to get back is:
                >>>/test.htm&parame ter=2
                >>>[/color]
                >>
                >>output="/"+input.spl it("/")[1][/color]
                >
                > gives:
                >
                > /test.htm&parame ter=2"><
                >[/color]
                oops
                Mick

                Comment

                • Dr John Stockton

                  #9
                  Re: Regular expression help

                  JRS: In article <Xns97519A551C8 A9eejj99@194.10 9.133.242>, dated Fri, 20
                  Jan 2006 14:10:13 remote, seen in news:comp.lang. javascript, Evertjan.
                  <exjxw.hannivoo rt@interxnl.net > posted :[color=blue]
                  >mick white wrote on 20 jan 2006 in comp.lang.javas cript:
                  >[color=green]
                  >> FrankIsHere@gma il.com wrote:[color=darkred]
                  >>> How can I strip out the text from within this "href" attribute?
                  >>>
                  >>> Here is the input:
                  >>> <span href="/test.htm&parame ter=2"></span>
                  >>>
                  >>> What I'd like to get back is:
                  >>> /test.htm&parame ter=2
                  >>>
                  >>>[/color]
                  >> output="/"+input.spl it("/")[1][/color]
                  >
                  >gives:
                  >
                  >/test.htm&parame ter=2"><[/color]

                  However, output = input.split('"' )[1] seems likely to do what is in
                  general necessary.

                  --
                  © 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

                  Working...