Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RexExp - Loop through $1 1

Status
Not open for further replies.

MarkZK

Technical User
Jul 13, 2006
202
GB
Hi all,

I'm trying to strip image tags using regular expression, I've had a few attempts now and have ended up using just one regexp , but, I don't think it's possible to loop through the parenthesized substrings (ie:$1) like I was hoping.

here is a quick messy example I was testing.

Code:
<script>
var string='<img src="[URL unfurl="true"]http://www.tipmaster.com/images/afro.gif"[/URL] id="blah"> test <br>  <img src="[URL unfurl="true"]http://www.tipmaster.com/images/afro2.gif">test[/URL] text,<img src="[URL unfurl="true"]http://www.tipmaster.com/images/bluegreedy.gif"[/URL] />  doesn&#39;t need stripping--> [URL unfurl="true"]http://www.tipmaster.com/images/neutral.gif[/URL] <a href="[URL unfurl="true"]http://example.com">link</a>';[/URL]

var reimgtag=/<img \S*(http:\/\/\S*(jpe?g|gif|bmp|png))\b[^>]*>/ig;
var matchreimg = string.match(reimgtag);

//alert(matchreimg[0].toString());
for(i=0;i<matchreimg.length;i++){
//alert(matchreimg);
//alert(RegExp.$1);

string = string.replace(matchreimg[i],' '+RegExp.$1+' ');
}
document.write(string+'<br /><br /> : <b>'+matchreimg.length);
</script>

and the out come was:

Code:
[URL unfurl="true"]http://www.tipmaster.com/images/bluegreedy.gif[/URL] test <br> text, doesn't need stripping--> [URL unfurl="true"]http://www.tipmaster.com/images/neutral.gif[/URL] <a href="[URL unfurl="true"]http://example.com">link</a>[/URL]

the out come I'm trying to achieve is:

Code:
[URL unfurl="true"]http://www.tipmaster.com/images/afro.gif[/URL] test <br>  [URL unfurl="true"]http://www.tipmaster.com/images/afro2.gif[/URL] test text, [URL unfurl="true"]http://www.tipmaster.com/images/bluegreedy.gif[/URL]  doesn't need stripping--> [URL unfurl="true"]http://www.tipmaster.com/images/neutral.gif[/URL] <a href="[URL unfurl="true"]http://example.com">link</a>[/URL]

if anyone could show me the right/proper way of doing this I'd be very grateful.

Thanks
 
You can do it like this. How do you want to separate the resultant match is not clear. I put \n\n herein-below for illustration. (I won't use string as variable name, not that it has any problem, it could easily slip into String if not careful.) Also, the string is coerced to use double quote to enclose the http, you probably have to relax that constraint to use more generally applicable to html string.
[tt]
var str,reimgtag;
str=[ignore]'<img src=" id="blah"> test <br> <img src=" text,<img src=" /> doesn&#39;t need stripping--> <a href="[/ignore];

reimgtag=/(<img\s+src\s*=\s*"\s*)(http:\/\/\S*\.(jpe?g|gif|bmp|png))("\s*[^>]*?>)\s*(.*?)(?=<img|$)/ig;
str=str.replace(reimgtag,"$2$5\n\n");
alert (str);
[/tt]
 
Perfect, I thought my Regexp was way off and you proved it :)


Thanks again Tsuji.
 
>I thought my Regexp was way off ...
Actually no, it is real close and the leading idea is good.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top