Solved

Regex replace text in string (ASP)

Posted on 2007-03-23
4
620 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:ddrudik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:exoska
ID: 18778409
 <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
 
LVL 27

Author Comment

by:ddrudik
ID: 18778938
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
 
LVL 3

Accepted Solution

by:
exoska earned 500 total points
ID: 18779307
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
 
LVL 27

Author Comment

by:ddrudik
ID: 18779743
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question