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

    Digital marketing without third-party cookies – new rules
    • Technology
    • Trends

    Digital marketing without third-party cookies – new rules

    February 21, 2024 by createIT
    eCommerce healthcheck
    • Services
    • Trends

    eCommerce healthcheck

    January 24, 2024 by createIT
    Live Visitor Count in WooCommerce with SSE
    • Dev Tips and Tricks

    Live Visitor Count in WooCommerce with SSE

    December 12, 2023 by createIT
    Calculate shipping costs programmatically in WooCommerce
    • Dev Tips and Tricks

    Calculate shipping costs programmatically in WooCommerce

    December 11, 2023 by createIT
    Designing a cookie consent modal certified by TCF IAB
    • Dev Tips and Tricks

    Designing a cookie consent modal certified by TCF IAB

    December 7, 2023 by createIT
    Understanding the IAB’s Global Vendor List (GVL)
    • Dev Tips and Tricks

    Understanding the IAB’s Global Vendor List (GVL)

    December 6, 2023 by createIT
    Effortlessly transform JSON data into a clear schema
    • Uncategorized

    Effortlessly transform JSON data into a clear schema

    December 5, 2023 by createIT
    Anatomy of WordPress malware: a technical deep dive
    • Dev Tips and Tricks
    • Technology

    Anatomy of WordPress malware: a technical deep dive

    December 4, 2023 by createIT
    Initializing CMP with the Stub Script and cmpapi.js
    • Dev Tips and Tricks

    Initializing CMP with the Stub Script and cmpapi.js

    November 29, 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