Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another regular expression. Getting the attributes of a html tag

Status
Not open for further replies.

alexandros12

Programmer
Oct 12, 2003
4
Hi there ! I want to make a regular expression in order to get the attributes from a html tag.

re.Pattern = "\s+(\w+)=""([^""]+)"""

the above is working almost well but it gets all strings that are like this ' aaa="bbb" ccc="ddd"'
now i want only to get these strings
'<aaa bbb=&quot;ccc&quot; ddd=&quot;eee&quot;'
i mean with a < in front of them.

i tried to do this :
&quot;<\w+\s+(\w+)=&quot;&quot;([^&quot;&quot;]+)&quot;&quot;&quot;
the problem is it only gets the first attribute of the tag and misses
all the next ones.

Is there a way to get all attributes ?

Do you know where else should i ask this question ?

Thanks!

 
You may perhaps play with 2 RegExp objects.
One to retrieve the html tag:
Code:
re1.Pattern=&quot;<([^>]+)>&quot;
and one to get the attributes in the precedent matches:
Code:
re2.Pattern=&quot;\s+(\w+)=&quot;&quot;([^&quot;&quot;]+)&quot;&quot;&quot;


Hope This Help
PH.
 
I was just testing some Reg Exp patterns in JavaScript so this is tested & working in JS :) - should port fine to VBscript.
JS:
Code:
var re =/<[a-z]+\s+([a-z]+=&quot;[^&quot;]+&quot;\s+)+/i
VBscript:
Code:
re.pattern =&quot;<[a-z]+\s+([a-z]+=&quot;&quot;[^&quot;&quot;]+&quot;&quot;\s+)+&quot;
re.ignoreCase = true

I've tested it with the string:
[[tt]click <a href=&quot; onclick=&quot;return someFunc('bla');&quot; bla>here</a>[/tt]]
and it matched:
[[tt]<a href=&quot; onclick=&quot;return someFunc('bla');&quot; [/tt]]

Good luck :)

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Slight improvement in the regexp:
Code:
var re =/<[a-z]+(\s+[a-z]+=&quot;[^&quot;]+&quot;)+/i

in VBscript:
Code:
re.pattern =&quot;<[a-z]+(\s+[a-z]+=&quot;&quot;[^&quot;&quot;]+&quot;&quot;)+&quot;

Matches the same

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top