macro to select HTML tag

Help with writing and playing macros

macro to select HTML tag

Postby StaticGhost » Thu May 27, 2010 1:09 am

Hi All,

I am trying to create a macro to select a HTML tag. E.g. if I select the first "a" and run the macro with this text:
Code: Select all
<a href="blah"><b>some text</b></a>

the whole tag (including angle brackets) will be selected.

I am aware that macros can't loop, so it wouldn't work for selecting the outer span in this situation:
Code: Select all
<span>some <span>text</span></span>

but that's ok - simple case first.. maybe I will invest the time to write a JavaScript to take care of the nested case.

From earlier posts, a regex works:
Code: Select all
<DIV[^>]*>([^ÿ]*?)</DIV>

But you can't put ^s in a regex..

A very poor non regex version is:
Code: Select all
InsertMode
ColumnModeOff
HexOff
UltraEditReOn
Find Select "</^s"


But this fails to select the outer angle brackets.

Any suggestions as to how to improve this would be most welcome!

Rob
:)
User avatar
StaticGhost
Basic User
Basic User
 
Posts: 35
Joined: Sun Nov 21, 2004 12:00 am

Re: macro to select HTML tag

Postby bulgrien » Thu May 27, 2010 1:26 am

StaticGhost wrote:you can't put ^s in a regex..


Sure, you can. You do it like this: \^s
User avatar
bulgrien
Master
Master
 
Posts: 92
Joined: Fri Dec 11, 2009 1:02 am
Location: Pennsylvania, USA

Re: macro to select HTML tag

Postby StaticGhost » Thu May 27, 2010 1:38 am

Hi Bulgrien,

bulgrien wrote:
StaticGhost wrote:you can't put ^s in a regex..

Sure, you can. You do it like this: \^s


No, that doesn't work - ^s means "selected text" but only in UltraEdit expressions, not perl/unix regex, which
Code: Select all
<DIV[^>]*>([^ÿ]*?)</DIV>
otherwise needs.. I did test it though, it doesn't work:

Code: Select all
InsertMode
ColumnModeOff
HexOff
PerlReOn
Find RegExp "<\^s[^>]*>([^ÿ]*?)</\^s>"


Rob
:)
User avatar
StaticGhost
Basic User
Basic User
 
Posts: 35
Joined: Sun Nov 21, 2004 12:00 am

Re: macro to select HTML tag

Postby bulgrien » Thu May 27, 2010 2:06 am

StaticGhost wrote:^s means "selected text" but only in UltraEdit expressions

Thanks for the clarification...
If I think of a solution that does work, I'll be sure to post it... :wink:
User avatar
bulgrien
Master
Master
 
Posts: 92
Joined: Fri Dec 11, 2009 1:02 am
Location: Pennsylvania, USA

Re: macro to select HTML tag

Postby bulgrien » Thu May 27, 2010 2:57 am

Try this macro:
Code: Select all
Find Select "</^s>"
Find Up Select "<"
User avatar
bulgrien
Master
Master
 
Posts: 92
Joined: Fri Dec 11, 2009 1:02 am
Location: Pennsylvania, USA

Re: macro to select HTML tag

Postby StaticGhost » Thu May 27, 2010 3:13 am

bulgrien wrote:Try this macro:
Code: Select all
Find Select "</^s>"
Find Up Select "<"

Nice one, and so simple - thanks Bulgrien!

Rob
:)
User avatar
StaticGhost
Basic User
Basic User
 
Posts: 35
Joined: Sun Nov 21, 2004 12:00 am

Re: macro to select HTML tag

Postby StaticGhost » Wed Jun 02, 2010 10:00 pm

OK, here is v2 that is a bit more flexible in dealing with nested tags. To use it, put the cursor in the first tag (don’t select it or copy it) and run the macro. It will still select only to the first close tag, but each time you re-run the macro (control+m) it will expand the selection to the next tag opening or closing. This way, you can count for yourself how many nested tags there are and stop when you need to.

Code: Select all
InsertMode
ColumnModeOff
HexOff
UltraEditReOn
IfSel
Find RegExp Select "</++^c>++"
Else
SelectWord
Copy
Find Select "</^c>"
Find Up Select "<"
EndIf

A more detailed explanation is on my blog post: UltraEdit macro to select HTML/XML tag.

Rob
:)

Edit 3/06/2010 2:28:02 PM Fixed bad c&p in macro text.
User avatar
StaticGhost
Basic User
Basic User
 
Posts: 35
Joined: Sun Nov 21, 2004 12:00 am

Re: macro to select HTML tag

Postby StaticGhost » Wed Jun 02, 2010 11:42 pm

And to do the same thing the other way, use this one.

Code: Select all
InsertMode
ColumnModeOff
HexOff
UltraEditReOn
IfSel
Find RegExp Up Select "</++^c>++"
Else
SelectWord
Copy
Find Up Select "</^c>"
Find Select ">"
EndIf

Personally, I have the select forward macro mapped to Control+Shift+. and the select backwards macro mapped to Control+Shift+,
User avatar
StaticGhost
Basic User
Basic User
 
Posts: 35
Joined: Sun Nov 21, 2004 12:00 am

Re: macro to select HTML tag

Postby bulgrien » Thu Jun 03, 2010 9:28 pm

By the way... if you really want to use a regular expression, you certainly can:

Code: Select all
PerlReOn
Find Up "<"
Key LEFT ARROW
Find RegExp "(?s)<([A-Z]+)[\s,>].*</\1>"


(?s)<([A-Z]+)[\s,>].*</\1>

(?s)
tells the regular expression to include newline characters when matching a . wildcard
< matches an open angle bracket
([A-Z]+) captures one or more alphabetical characters to use in the backreference: \1
[\s,>] indicates that the character following the captured text should be whitespace or a closed angle bracket
And .*</\1> of course matches everything until the backreference is found in a closing tag

It might be possible to detect and skip imbedded nodes using regex lookarounds... but I haven't tried to do so.
User avatar
bulgrien
Master
Master
 
Posts: 92
Joined: Fri Dec 11, 2009 1:02 am
Location: Pennsylvania, USA


Return to Macros

cron