Link to home
Start Free TrialLog in
Avatar of ddrudik
ddrudikFlag for United States of America

asked on

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.
Avatar of exoska
exoska

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

Avatar of ddrudik

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of exoska
exoska

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ddrudik

ASKER

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.