...

View Full Version : Resolved Regex problem driving me nuts!



Krupski
09-20-2011, 12:59 AM
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):



<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" />
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:



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

Old Pedant
09-20-2011, 02:29 AM
Aren't you working too hard?


txt = txt.replace(/\<img\s.*?alt\=\"\:([^\:]+)\:\"[^\>]*\>/gi,':$1:');

???

Untested. Let me try it.

Yeah. seems right.



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

Krupski
09-20-2011, 03:44 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum