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

    JS react developer – 20 questions for a job interview
    • Technology
    • Trends

    JS react developer – 20 questions for a job interview

    May 24, 2023 by createIT
    PHPStorm – how to replace HTML tags using regex?
    • Dev Tips and Tricks

    PHPStorm – how to replace HTML tags using regex?

    May 18, 2023 by createIT
    Ultimate GDPR – get user consent logs
    • Dev Tips and Tricks

    Ultimate GDPR – get user consent logs

    May 5, 2023 by createIT
    Backend Developer – how to become one?
    • Technology
    • Trends

    Backend Developer – how to become one?

    May 5, 2023 by createIT
    SEO Checklist for website migration
    • Services

    SEO Checklist for website migration

    April 4, 2023 by createIT
    A game changer for WooCommerce users
    • Our Highlights

    A game changer for WooCommerce users

    March 24, 2023 by createIT
    Write content that sells
    • Trends

    Write content that sells

    March 21, 2023 by createIT
    The differences between a web app and a mobile app
    • Services
    • Technology
    • Trends

    The differences between a web app and a mobile app

    March 7, 2023 by createIT
    Webrooming and showrooming
    • Trends

    Webrooming and showrooming

    February 14, 2023 by createIT
    PHPStorm – fix long load time of a directory window
    • Dev Tips and Tricks

    PHPStorm – fix long load time of a directory window

    January 20, 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