regular expression help

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

    regular expression help

    Hi all,

    given a string:

    " <img class='myclass' alt='my picture' src='image.jpg' /"

    (where alt and src are changeable)

    how do I create the following string:

    " <div class='containe r'>
    <div class='inner'>
    <img class='myclass' alt='my picture' src='image.jpg' />
    <p>my picture</p>
    </div>
    </div"

    (where the contents of <p></pare the same as the image 'alt') ?


    I believe a regular expression would solve this but I'm not sure on the
    syntax.

    something like:

    regex.replace(" <img class='myclass' alt='$1' src='$2' />","<div
    class='containe r'><div class='inner'>< img class='myclass' alt='$1'
    src='$2' /><p>$1</p></div></div>")


    Any ideas? Thanks!

    --

    fiddlewidawiddu m
  • Evertjan.

    #2
    Re: regular expression help

    Stimp wrote on 02 aug 2008 in comp.lang.javas cript:
    Hi all,
    >
    given a string:
    >
    " <img class='myclass' alt='my picture' src='image.jpg' /"
    >
    (where alt and src are changeable)
    >
    how do I create the following string:
    >
    " <div class='containe r'>
    <div class='inner'>
    <img class='myclass' alt='my picture' src='image.jpg' />
    <p>my picture</p>
    </div>
    </div"
    >
    (where the contents of <p></pare the same as the image 'alt') ?
    >
    >
    I believe a regular expression would solve this but I'm not sure on the
    syntax.
    >
    something like:
    >
    regex.replace(" <img class='myclass' alt='$1' src='$2' />","<div
    class='containe r'><div class='inner'>< img class='myclass' alt='$1'
    src='$2' /><p>$1</p></div></div>")
    >
    >
    Any ideas? Thanks!
    >
    Please do not multipost

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

    Comment

    • Stimp

      #3
      Re: regular expression help

      On Sat, 2 Aug 2008 Evertjan. <exjxw.hannivoo rt@interxnl.net wrote:
      Stimp wrote on 02 aug 2008 in comp.lang.javas cript:
      >
      >Hi all,
      >>
      >given a string:
      >>
      >" <img class='myclass' alt='my picture' src='image.jpg' /"
      >>
      >(where alt and src are changeable)
      >>
      >how do I create the following string:
      >>
      >" <div class='containe r'>
      > <div class='inner'>
      > <img class='myclass' alt='my picture' src='image.jpg' />
      > <p>my picture</p>
      > </div>
      > </div"
      >>
      >(where the contents of <p></pare the same as the image 'alt') ?
      >>
      >>
      >I believe a regular expression would solve this but I'm not sure on the
      >syntax.
      >>
      >something like:
      >>
      >regex.replace( "<img class='myclass' alt='$1' src='$2' />","<div
      >class='contain er'><div class='inner'>< img class='myclass' alt='$1'
      >src='$2' /><p>$1</p></div></div>")
      >>
      >>
      >Any ideas? Thanks!
      >>
      >
      Please do not multipost
      it's a question relevant to different groups, so I've posted to those
      where the answer would most likely be found.

      Of course if an answer appears in another group, I'd post the answer
      here as well
      --

      fiddlewidawiddu m

      Comment

      • Evertjan.

        #4
        Re: regular expression help

        Stimp wrote on 02 aug 2008 in comp.lang.javas cript:
        On Sat, 2 Aug 2008 Evertjan. <exjxw.hannivoo rt@interxnl.net wrote:
        >Stimp wrote on 02 aug 2008 in comp.lang.javas cript:
        >>Any ideas? Thanks!
        >>>
        >>
        >Please do not multipost
        >
        it's a question relevant to different groups, so I've posted to those
        where the answer would most likely be found.
        >
        Of course if an answer appears in another group, I'd post the answer
        here as well
        Please first learn Netiquette.

        If you feel you must post to different NG's for that reason,
        you could crosspost, not multipost.



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

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: regular expression help

          Stimp wrote:
          On Sat, 2 Aug 2008 Evertjan. <exjxw.hannivoo rt@interxnl.net wrote:
          >Please do not multipost
          >
          it's a question relevant to different groups, so I've posted to those
          where the answer would most likely be found.
          Maybe you should get a minimum clue about Usenet first?

          <http://en.wikipedia.or g/wiki/Crossposting>

          Your slrn does support this very well.
          Of course if an answer appears in another group, I'd post the answer
          here as well
          This would be unnecessary had you crossposted.

          As for your question: Google is your friend.


          PointedEars
          --
          Anyone who slaps a 'this page is best viewed with Browser X' label on
          a Web page appears to be yearning for the bad old days, before the Web,
          when you had very little chance of reading a document written on another
          computer, another word processor, or another network. -- Tim Berners-Lee

          Comment

          • Stimp

            #6
            Re: regular expression help

            On Sat, 2 Aug 2008 Evertjan. <exjxw.hannivoo rt@interxnl.net wrote:
            Stimp wrote on 02 aug 2008 in comp.lang.javas cript:
            >
            Please first learn Netiquette.
            >
            If you feel you must post to different NG's for that reason,
            you could crosspost, not multipost.
            cross-posting is far worse tbh... why should I post a message here where
            the resulting-message will appear in a different newsgroup?

            That's bad netiquette.

            Incidentally, it's good netiquette to delete quoted text from previous
            posts. Something you've neglected to do I notice.

            --

            fiddlewidawiddu m

            Comment

            • Stimp

              #7
              Re: regular expression help

              On Sat, 2 Aug 2008 Stimp <ren@spumco.com wrote:
              On Sat, 2 Aug 2008 Evertjan. <exjxw.hannivoo rt@interxnl.net wrote:
              >Stimp wrote on 02 aug 2008 in comp.lang.javas cript:
              >
              cross-posting is far worse tbh... why should I post a message here where
              the resulting-message will appear in a different newsgroup?
              turns out cross-posting send answers to all groups .. I wasn't aware of
              that, but good to know. :)

              --

              fiddlewidawiddu m

              Comment

              • Stimp

                #8
                Re: regular expression help

                On Sat, 2 Aug 2008 Thomas 'PointedEars' Lahn <PointedEars@we b.dewrote:
                Stimp wrote:
                >On Sat, 2 Aug 2008 Evertjan. <exjxw.hannivoo rt@interxnl.net wrote:
                >>Please do not multipost
                >>
                >it's a question relevant to different groups, so I've posted to those
                >where the answer would most likely be found.
                >
                Maybe you should get a minimum clue about Usenet first?
                haha pointedears .. you still haven't changed :)

                been several years since I was last in this newsgroup and you're still a
                pedantic miserable git (judging by the replies you're giving others)

                --

                fiddlewidawiddu m

                Comment

                • Dr J R Stockton

                  #9
                  Re: regular expression help

                  In comp.lang.javas cript message <slrng999f9.nnj .ren@minerva.re dbrick.dcu
                  ..ie>, Sat, 2 Aug 2008 18:23:41, Stimp <ren@spumco.com posted:
                  >Hi all,
                  >
                  >given a string:
                  >
                  >" <img class='myclass' alt='my picture' src='image.jpg' /"
                  >
                  >(where alt and src are changeable)
                  >
                  >how do I create the following string:
                  >
                  >" <div class='containe r'>
                  <div class='inner'>
                  <img class='myclass' alt='my picture' src='image.jpg' />
                  <p>my picture</p>
                  </div>
                  </div"
                  >
                  >(where the contents of <p></pare the same as the image 'alt') ?
                  >
                  >
                  >I believe a regular expression would solve this but I'm not sure on the
                  >syntax.
                  Google will find RegExp references, and FAQ 4.16 would lead you to them
                  <FAQENTRY>The SUN page is ill-set and nicer versions can be found
                  </FAQENTRY>.

                  An example is inadequate without a clear expression of the requirement
                  in general terms.

                  Making such a clear statement will enable you to reduce the problem, at
                  least.

                  Your need seems to be to take a line, find a string in it, and to create
                  a string of the form
                  constant-prefix input-string postfix-1 found-string postfix-2
                  and that is most clearly done by manifest concatenation (though possible
                  with a single S.replace(RegEx p)).

                  Consider, therefore,

                  S = " <img class='myclass' alt='my picture' src='image.jpg' /"
                  M = S.match(/alt='([^']*)'/)
                  if (!M) alert("not found")
                  Found = M[1]
                  Prfx = "Prefix \n " ; Post1 = " P1 " ; Post2 = " P2 "
                  Out = Prfx + S + Post1 + Found + Post2

                  However, you should in the RegExp change = to \s*=\s* unless certainly
                  not needed; and you should consider whether there is any real
                  possibility of alt= being found elsewhere in the string; and you should
                  similarly consider whether the relevant quotes might instead be ".

                  It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

                  --
                  (c) John Stockton, nr London UK. ?@merlyn.demon. co.uk IE7 FF2 Op9 Sf3
                  news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
                  <URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
                  <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.

                  Comment

                  • Phil

                    #10
                    Re: regular expression help

                    Stimp wrote:
                    given a string:
                    >
                    " <img class='myclass' alt='my picture' src='image.jpg' /"
                    Is this really just a string, or is it part of a DOM object you've
                    extracted and turned into a string? If it's the latter, you'd be much
                    better off using DOM methods to manipulate it directly, because as Dr.
                    Stockton pointed out, if one comes along that's
                    " <img class = 'myclass' alt = 'my picture' src = 'image.jpg' /" or
                    " <img alt='my picture' class='myclass' src='image.jpg' /" or
                    ' <img class="myclass" alt="my picture" src="image.jpg" /' or
                    " <img class='myclass' alt='my picture' src='image.jpg' " or
                    any of dozens of other variations, you're screwed.

                    Comment

                    • Stimp

                      #11
                      Re: regular expression help

                      On Thu, 7 Aug 2008 Phil <wrote:
                      Stimp wrote:
                      >
                      >given a string:
                      >>
                      >" <img class='myclass' alt='my picture' src='image.jpg' /"
                      >
                      Is this really just a string, or is it part of a DOM object you've
                      extracted and turned into a string? If it's the latter, you'd be much
                      better off using DOM methods to manipulate it directly, because as Dr.
                      Stockton pointed out, if one comes along that's
                      " <img class = 'myclass' alt = 'my picture' src = 'image.jpg' /" or
                      " <img alt='my picture' class='myclass' src='image.jpg' /" or
                      ' <img class="myclass" alt="my picture" src="image.jpg" /' or
                      " <img class='myclass' alt='my picture' src='image.jpg' " or
                      any of dozens of other variations, you're screwed.
                      nah, won't be any other variations since I'm creating the string myself
                      and substituting the src etc.
                      --

                      fiddlewidawiddu m

                      Comment

                      Working...