Get a free advice now!

    Pick the topic

    Developer OutsourcingWeb developingApp developingDigital MarketingeCommerce systemseEntertainment systems

    Thank you for your message. It has been sent.

    Tags

    PHPStorm – how to replace HTML tags using regex?

    PHPStorm – how to replace HTML tags using regex?

    Challenge: search for the last table column and replace it with a new value

    Solution: use proper regex and click ‘Replace All’

    We have an HTML document with table data. We need to remove the last column. We could do it manually, but our table has over 200 rows. How to automate the “search and replace” job?

    PHPStorm includes an option to find a particular string using a regex formula. Let’s formulate a proper one. The column for removal is placed as the last element in TR tags. It always contains a number value. We should also remember that TD elements are preceded by empty spaces.

    Regex for the last column

    We’re going to use regex that will find table cells with numbers, we’re also looking for the closing TR tag. As a new replacement, we are going to use the closing TR tag.

    Here is the final solution:

    Search for:

            <td>[+-]?([0-9]*[.])?[0-9]+</td>
        </tr>
    

    Replace with:

        </tr>

    Enable the regex replacement

    Make sure to enable Regex mode icon (it’s next to the search field. Look for a dot with the asterisk icon).

    Dark window with code and an arrow pointing to one of the options

    Example table with data

    Lastly, here is the HTML source from our example.

    <table width="900px">
        <tbody>
        <tr>
            <th width="150px">First Name</th>
            <th width="150px">Last Name</th>
            <th width="150px">Age</th>
            <th width="150px">Education</th>
            <th width="150px">Occupation</th>
            <th width="150px">last_col</th>
        </tr>
        <tr>
            <td>Max</td>
            <td>Thomas</td>
            <td>26</td>
            <td>Upper secondary</td>
            <td>Composer</td>
            <td>967</td>
        </tr>
        <tr>
            <td>Adison</td>
            <td>Cameron</td>
            <td>24</td>
            <td>Upper secondary</td>
            <td>Producer</td>
            <td>910</td>
        </tr>
        </tbody>
    </table>
    
    Dark window with colorful code

    That’s it for today’s tutorial. Be sure to follow us for other useful tips and guidelines.

    Comments
    0 response

    Add comment

    Your email address will not be published. Required fields are marked *

    Popular news

    Streamlining Business Growth: Leveraging Discovery for SME Success
    • Services
    • Trends

    Streamlining Business Growth: Leveraging Discovery for SME Success

    September 11, 2023 by createIT
    Benefits of using WordPress
    • Technology
    • Trends

    Benefits of using WordPress

    June 30, 2023 by createIT
    Find out which project flow will suit your idea
    • Services
    • Trends

    Find out which project flow will suit your idea

    June 9, 2023 by createIT
    How to become a Frontend Developer? Essential tips
    • Technology
    • Trends

    How to become a Frontend Developer? Essential tips

    June 6, 2023 by createIT
    Host React App with Netlify
    • Dev Tips and Tricks
    • Technology

    Host React App with Netlify

    May 31, 2023 by createIT

    Support – Tips and Tricks
    All tips in one place, and the database keeps growing. Stay up to date and optimize your work!

    Contact us