Forum Moderators: open

Message Too Old, No Replies

strip html while extracting links w js

strip tags save href

         

phazei

10:46 am on Jul 29, 2007 (gmt 0)

10+ Year Member



I'm using this
temp = temp.replace(/(<([^>]+)>)/ig,"");
to strip html tags which works well, except...

temp = "<b>hi there,<a href='www.example.com'>i'm a link</a>";
I would like that to turn into
"hi there, www.example.com i'm a link"

I've been googling for quite a while and I can't seem to find any solution.

I'd guess I'd need to search temp for href, grab the next item that's in quotes, spit it out next to it, then do the regexp.

I'm just not sure how to do that exactally.

Can I treat the string as an array of characters? I could go through one at a time, find href, use recursion, go backwards, spit the link out right before the '<a' and continue forward skipping the next href. Even if I can do that, I'm not sure how to insert the item in the string.

I'd also perhaps want to do the same thing with src in imgs.

[EDIT]
I was thinking after I originally posted this (in wrong forum)

If I can assign variables to items found in a regular expression, then I could use something like...

temp = temp.replace(/(<a * href="$1" *>)/ig,$1);

Umm, is there a proper way to do that?

Thanks :)
Adam

phazei

11:22 am on Jul 29, 2007 (gmt 0)

10+ Year Member



temp = temp.replace(/<a[^href]+href=\"([^\"]*)\"[^>]*>/ig,"$1");

WOOHOO :D

That works.
Took forever to figure out the ( ) needed to go around the item for the variable to match.