Colorize negative numbers in table

by magizter

HTML

<table class="table table-striped table-bordered table-sm table-hover tablefilter" id="accounts">
        <thead class="thead-gray">
        <tr>
            <th scope="col">Group</th>
            <th scope="col">Account ID</th>
            <th scope="col" colspan="2">DisplayName</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>463456</td>
            <td>1210701</td>
            <td>755235</td>
            <td class="text-center"><a href="#"><i class="fa fa-arrow-circle-right" aria-hidden="true"></i></a></td>
        </tr>
        <tr>
            <td>56456446</td>
            <td>1210703</td>
            <td>-4565456</td>
            <td class="text-center"><a href="#"><i class="fa fa-arrow-circle-right" aria-hidden="true"></i></a></td>
        </tr>
        <tr>
            <td>-54656</td>
            <td>1210699</td>
            <td>57875</td>
            <td class="text-center"><a href="#"><i class="fa fa-arrow-circle-right" aria-hidden="true"></i></a></td>
        </tr>
        <tr>
            <td>-644646</td>
            <td>1210713</td>
            <td>-155</td>
            <td class="text-center"><a href="#"><i class="fa fa-arrow-circle-right" aria-hidden="true"></i></a></td>
        </tr>

        <tr>
            <td>45456456</td>
            <td>1210706</td>
            <td>4545453</td>
            <td class="text-center"><a href="#"><i class="fa fa-arrow-circle-right" aria-hidden="true"></i></a></td>
        </tr>
        </tbody></table>

CSS

.neg { color: #f00; };

JavaScript

// Colorize negative numbers
    $('td').each(function() {
        var val = $(this).text(), n = +val;
        if (!isNaN(n) && /^\s*[+-]/.test(val)) {
            $(this).addClass(val >= 0 ? '' : 'neg')
        }
    });