Parsing anchor tags in HTML w/ certain attributes and replacing w/ new attributes

Micah

Member
Joined
Mar 7, 2005
Messages
5
Hello all.

i am having this problem. i have got to correct a whole site worth of links. i want to basically check them to see if they have the title attribute and do not contain onMouse[XXX] events and replace the attributes with an added mouse events that will contain JS statements to change the status bar to what was in the title attribute. i hope this makes sense.

here is an example

<a href="www.somewhere.com" title="Click me now, please." class="something" >

i would want the expression to evaluate to this

<a href="www.somewhere.com" title="Click me now, please." class="something" onMouseOver="window.status=Click me now, please.;return true;" onMouseOut="window.status=;return true;">

Is this even possible?? i have played with it a bit, but honestly i was not even sure if it was possible :confused: . i am hoping someone can help please.

thanks in advance!
 
heres some stuff that will help.

find the title
title=(?<!\\)(\\\\)*"(?<title>.*?)(?<!\\)(\\\\)*"

find the onmouseXXX
(?<onName>onMouse[^=]+)=(?<!\\)(\\\\)*"(?<onAction>.*?)(?<!\\)(\\\\)*"

youll need a regex to find the anchors, I didnt make one because I dont know if you have nested anchors, well heres a simple one.

(?i)(<a.*?>)|(<a>.*?</a>)

I think youll also find the MatchEvaluator delegate useful.
 
thanks for the reply. that is a great use of neg lookbehinds.

that is a great basis for me to work with.

do you know if there is a way for me to combine them into one exression with conditional expressions??

-Micah
 
I would, but the problem is, I dont know if the title will always come before the onmouseXXX, and vice versa. To do it in 1 regex, I would combine the two regexes using the |
(regexA regexB)|(regexB regexA)
 
Back
Top