• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 768
  • Last Modified:

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
0
jlm151
Asked:
jlm151
  • 3
  • 3
1 Solution
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now