Regex replace text in string (ASP)

I have a string that contains HTML page content, scattered throughout will be HREF's in the format of:
  <A HREF="text here ending in a /">test</A>
  <A HREF="text here ending in something else">test</A>

I would like to construct the most efficient regex replace function possible that would insert the string:
  onclick="myevent;"
in the 'A HREF' tags where the the location text ends in a "/", such as:
  <A HREF="text here ending in a /" onclick="myevent;">

The remaining 'A HREF' tags where the location text did not end in "/" would not be modified.
LVL 27
ddrudikAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

exoskaCommented:
 <A HREF="text here ending in a /">test</A> asfsaf asf s sa
  asdfsadfs<A HREF="text here ending in something else">test</A> sadf asdf asf saf <A HREF="text here ending in a /">test</A>
  <A HREF="text here ending in something else">test</A>

lets say this is the mixed up text

and this is the try;

Dim ResultString, myRegExp
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Multiline = True
myRegExp.Global = True
myRegExp.Pattern = "<(a*)([^/]*)/"">(.*?)</\1>"
ResultString = myRegExp.Replace(SubjectString, "<$1 $2 /"" onclick=""myevent;"">$3</$1>")

please take care of the result and the subject strings..

0
ddrudikAuthor Commented:
That didn't seem to work for me, as no text is added to the references when tested.

This A HREF where the location reference does not end in '/':
<A HREF="?c=a&d=/testfile.foo">
Should be replaced with:
<A HREF="?c=a&d=/testfile.foo" onclick="myevent;">

This A HREF where the location reference ends in '/':
<A HREF="?c=a&d=/">
Should not be modified.

Thanks for your help.
0
exoskaCommented:
oh vice-versa( or a little confusing :))

well if you want to replace that type ( <A HREF="?c=a&d=/testfile.foo"> ) below works

Dim ResultString, myRegExp
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Multiline = True
myRegExp.Global = True
myRegExp.Pattern = "<(a*)([^/]*)/([A-Za-z0-9]?[^>]+)"">"
ResultString = myRegExp.Replace(SubjectString, "<$1 $2$3/"" onclick=""myevent;"">")

it does not replace <A HREF="?c=a&d=/"> type

im not sure so if it s like  a <A HREF="?c=a&d=/testfile.foo">test</a> type the one
below works( but closing tag(</A>) is required for this one)
'the one with the closing tag
Dim ResultString, myRegExp
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Multiline = True
myRegExp.Global = True
myRegExp.Pattern = "<(a*)([^/]*)/([A-Za-z0-9]?[^>]+)"">([^<]+)(</\1>)"
ResultString = myRegExp.Replace(SubjectString, "<$1 $2$3/"" onclick=""myevent;"">$4$5")

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ddrudikAuthor Commented:
The first one makes no string replacement and the second one make changes to all A HREF references regardless of if they end in a "/" or not  (remember I need the string only added to those A HREF's that do not end in a "/".  I also had to remove that "/" in the replace command since I don't want a trailing "/" to be added to all references.

I think I can work with your second one by just adding another replacement later for all:
/" onclick="myevent;" with nothing in order to remove the extra ones added by the regex.

If you come up with a modification so I don't have to add another replace that would be better.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.