Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts

    Regex problem driving me nuts!

    Hi all,

    I have a string which contains HTML and *possibly* IMG tags. I am trying to extract part of each IMG tag which may be in the string. The string is part of a chat-box message which may or may not contain smilies.

    Here is a typical line of text, without processing (note I mis-spell the bbcode "quote" so as not to fool this board):

    Code:
    [qoute="Krupski"]<img src="./images/smilies/smile.gif" alt=":smile:" title="smile" />  <img src="./images/smilies/thumbsup.gif" alt=":thumbsup:" title="thumbsup" />  <img src="./images/smilies/thumbsdown.gif" alt=":thumbsdown:" title="thumbsdown" />[/qoute]
    Notice that each image has an "ALT" attribute which contains the smiley code of the smiley image. THAT is what I want to extract.

    This is the regex I am using to grab each smiley code:

    Code:
    txt = txt.replace(/<img(?:.*?):(.*?):(?:.*?)\/>/gi,':$1:');
    The weird thing is, the regex grabs the FIRST and LAST of the three, but not the middle one!

    I'm sure the stupid mistake is sitting right in front of my face, but I'm not seeing it.

    Any help will be greatly appreciated!

    Thanks.

    -- Roger
    Last edited by Krupski; 09-20-2011 at 03:45 AM.
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,496 Times in 4,460 Posts
    Aren't you working too hard?
    Code:
    txt = txt.replace(/\<img\s.*?alt\=\"\:([^\:]+)\:\"[^\>]*\>/gi,':$1:');
    ???

    Untested. Let me try it.

    Yeah. seems right.

    Code:
    <html>
    <head>
    <script>
    function test()
    {
        var txt = document.getElementById("here").innerHTML;
        txt = txt.replace(/\<img\s.*?alt\=\"\:([^\:]+)\:\"[^\>]*\>/gi,':$1:');
        document.getElementById("there").innerHTML = txt;
    }
    </script>
    </head>
    <body>
    <div id="here">
    <img src="./images/smilies/smile.gif" alt=":smile:" title="smile" />
    <img src="./images/smilies/thumbsup.gif" alt=":thumbsup:" title="thumbsup" />
    <img src="./images/smilies/thumbsdown.gif" alt=":thumbsdown:" title="thumbsdown" />
    </div>
    <form><input type="button" value="test" onclick="test()" /></form>
    <br/>
    <hr/>
    <span id="there"></span>
    <hr/>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Krupski (09-20-2011)

  • #3
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by Old Pedant View Post
    Aren't you working too hard?
    Well, good news and bad news.

    Good news: Yours also works.
    Bad news: It does the same thing as mine!

    ...implying...

    That the problem is elsewhere

    ....leading me to look for the real problem...

    Good news: Which I have found.

    It was a mistake in a different piece of my code... I had doubly escaped a single-quote which worked because (I think) the browser was hiding the error. The \\' I think the browser saw as \' and said "OK he means '... which worked until certain other characters were mixed in with it.

    Thanks!

    -- Roger
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •