ASP RegEx to Split Apart an Anchor Tag

Hello,
I'm looking for a a few regular expressions that I can use to split apart an anchor tag (ie. href, alt, target, link text, etc)

For example:
<a href="http://www.blah.com" alt=blah target='_blank'>MyText</a> (attributes may or may not be wrapped in quotes)

To pull the target out, I'm using the following expression:

RegEx.Pattern="(^|.*\s)*target=(.*)[\s\S*=.*^>]|[>.*<.*]"
strTarget=RegEx.Replace(strLink, "$2")
strTarget=Trim(Replace(strTarget, """", ""))

It only works in some scenarios. I need it to work if target is the first, middle, last attribute in the tag. And need to do the same thing for the href, alt, etc.

Thanks in advance!
John
jlm151Asked:
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.

WMIFCommented:
it appears that you have already singled out the anchor tags and you only want the information about each of the attributes, is that correct?  i would just like to clarify before i dig in.
0
jlm151Author Commented:
Yep
0
clockwatcherCommented:
Are you actually trying to strip the attributes out or are you just trying to find their values?  Your question seems like you just want to find the values.  But, then you went with a replace?  Anyway, if you're just looking to find the values then:

<%

function GetAttr(attrib, tag)

     dim retval
     
     dim r
     set r = new RegExp
     r.pattern = attrib & "=\s*?['""]?(.*?)['""\s>]"
     r.global = true
     r.ignorecase = true

     dim matches
     set matches = r.execute(strLink)
     if matches.count > 0 then
          retval = matches(0).SubMatches(0)
     end if

     GetAttr = retval

end function

strLink = "<a href=""http://www.blah.com"" alt=blah target='_blank'>MyText</a> (attributes may or may not be wrapped in quotes)"

find = Array("href","alt","target")

for i = lbound(find) to ubound(find)
     response.write find(i) & " -- " & GetAttr(find(i),strLink) & "<BR>"
next
%>
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

clockwatcherCommented:
Sorry that pattern should be changed to:

 r.pattern = attrib & "=\s*?['""]?(.*?)['""][\s>]"

Made a change to shorten it up a bit when I posted and forgot why I had it set originally.

Also it assumes you've got a single anchor tag and returns the first attribute match.  In other words, if you've got some funky tags:

  <a href="here" href="there">href=where</a>

And you're search for href, you'll only get back the first match -- which in this case would be 'here'.
0
clockwatcherCommented:
I swear I must be out of it tonight.  Try:

function GetAttr(attrib, tag)

     dim retval
     
     dim r
     set r = new RegExp
     r.pattern = attrib & "=(?:\s*['""](.*?)['""])|" & attrib & "=(?:\s*(.*?)[\s>]+)"
     r.global = true
     r.ignorecase = true

     dim matches, m1, m2
     set matches = r.execute(strLink)
     if matches.count > 0 then
          retval = matches(0).SubMatches(0)
       if matches(0).submatches.count > 1 then
            if matches(0).submatches(1) <> "" then retval = matches(0).submatches(1)
       end if
     end if

     GetAttr = retval
end function
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
jlm151Author Commented:
still no dice...but I'll have to admit your regex is alot cleaner and makes more sense than mine. I'll keep toying around with it.
0
jlm151Author Commented:
found my problem, spaces between the attribute and the "=" sign. Altered the regex to:
r.pattern = attrib & "\s*=(?:\s*['""](.*?)['""])|" & attrib & "\s*=(?:\s*(.*?)[\s>]+)"

I reaaallly appreciated your help ClockWatcher....learned something new about regular expressions :-)

Thanks again!
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
ASP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.