How changing several similar HTML tags by RegEx?



  • 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



  • 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.



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

    You are my hero, thanks very much, working!


Log in to reply