Community
    • Login

    How changing several similar HTML tags by RegEx?

    Scheduled Pinned Locked Moved General Discussion
    3 Posts 2 Posters 628 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • bert maierB Offline
      bert maier
      last edited by

      Hello to all, my newbie post…

      How changing several similar HTML tags by RegEx?

      Starting point:
      <li class=“h6”> including various text1, tags and so on </li>
      something else1
      <li class=“h6”> including various text2, tags and so on </li>
      something else2
      <li class=“h6”> including various text3, tags and so on </li>

      I want to change all to:
      <h6> including various text1, tags and so on </h6>
      something else1
      <h6> including various text2, tags and so on </h6>
      something else2
      <h6> including various text3, tags and so on </h6>

      First problem:
      If using RegEx <li class=“h6”>(.*)</li>
      first <li class=“h6”> and last/third </li> is found, but only if “find \r and \n” is activated.
      But closing next </li> corresponding to it’s opening tag <li class=“h6”> must be found.

      Second problem:
      Search-and-replace shall not alter different included text1, text2, text3 between the opening <h6> and corresponding closing </h6>.

      How to get this working?

      Thanks for help and kind regards

      1 Reply Last reply Reply Quote 0
      • PeterJonesP Offline
        PeterJones
        last edited by

        With

        • Find = <li class="h6">(.*)</li>
        • Replace = <h6>$1</h6> – the $1 takes the contents from the first matched parentheses group, hence “shall not alter different included text”
        • ☐ . matches newline disabled

        It should work as desired – the included text stays the same, but each pair of <li...>...</li> changes.

        With

        • Find = <li class="h6">(.*)</li>
        • Replace = <h6>$1</h6>
        • ☑ . matches newline enabled

        it will match the first <li ...> and the last </li>, which is not what you want

        With

        • Find = <li class="h6">(.*?)</li> – the ? limits the .* to the smallest possible match
        • Replace = <h6>$1</h6>
        • ☑ . matches newline enabled

        it will match each <li ...> and its paired </li>, similar to the first example.

        Either the first or the third should work for you.

        This animation shows those three examples in practice, with the first and third working the way I think you want:

        Please note that nested li will mess it up: for example, <li class="h6">blah <li class="embeded">yada<li> blah</li> will not replace correctly. Adding nesting to the regex will be nasty; if you need it, @guy038 should be able to help; otherwise, hopefully what I have will work for you.

        bert maierB 1 Reply Last reply Reply Quote 1
        • bert maierB Offline
          bert maier @PeterJones
          last edited by

          @PeterJones said in How changing several similar HTML tags by RegEx?:

          You are my hero, thanks very much, working!

          1 Reply Last reply Reply Quote 1

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors