macro to select HTML tag

macro to select HTML tag

44
Basic UserBasic User
44

    May 27, 2010#1

    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
    :)
    UltraEdit - licensed for life!

    http://robertmarkbramprogrammer.blogspot.com

    901
    MasterMaster
    901

      May 27, 2010#2

      StaticGhost wrote:you can't put ^s in a regex..
      Sure, you can. You do it like this: \^s

      44
      Basic UserBasic User
      44

        May 27, 2010#3

        Hi Bulgrien,

        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
        :)
        UltraEdit - licensed for life!

        http://robertmarkbramprogrammer.blogspot.com

        901
        MasterMaster
        901

          May 27, 2010#4

          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:

            May 27, 2010#5

            Try this macro:

            Code: Select all

            Find Select "</^s>"
            Find Up Select "<"

            44
            Basic UserBasic User
            44

              May 27, 2010#6

              bulgrien wrote:Try this macro:

              Code: Select all

              Find Select "</^s>"
              Find Up Select "<"
              Nice one, and so simple - thanks Bulgrien!

              Rob
              :)

                Jun 03, 2010#7

                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 18/04/2015: An improved version of this macro was posted at macro to select HTML tag v2.

                  Jun 03, 2010#8

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

                  Edit 18/04/2015: An improved version of this macro was posted at macro to select HTML tag v2.
                  UltraEdit - licensed for life!

                  http://robertmarkbramprogrammer.blogspot.com

                  901
                  MasterMaster
                  901

                    Jun 04, 2010#9

                    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 embedded nodes using regex lookarounds, but I haven't tried to do so.