jlm151
asked on
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)*ta rget=(.*)[ \s\S*=.*^> ]|[>.*<.*] "
strTarget=RegEx.Replace(st rLink, "$2")
strTarget=Trim(Replace(str Target, """", ""))
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
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>
To pull the target out, I'm using the following expression:
RegEx.Pattern="(^|.*\s)*ta
strTarget=RegEx.Replace(st
strTarget=Trim(Replace(str
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
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.
ASKER
Yep
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
%>
<%
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>
find = Array("href","alt","target
for i = lbound(find) to ubound(find)
response.write find(i) & " -- " & GetAttr(find(i),strLink) & "<BR>"
next
%>
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'.
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'.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
ASKER
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!
r.pattern = attrib & "\s*=(?:\s*['""](.*?)['""]
I reaaallly appreciated your help ClockWatcher....learned something new about regular expressions :-)
Thanks again!