2

I have values in my HTML like below

348419 ( 26% )
99425 ( -11% )

I would like them to change the text color depending on the values in parenthesis. If the value is positive, it should be displayed in Green, else red.

I'm using the below code, however the text is always displayed in Green as all the starting values such as 348419,99425 are positive. It is not able to parse the values in brackets. Can someone pls help.

var cells = document.querySelectorAll('#tableID td');

for ( var i=0; i<cells.length; i++ ) {
var cell    = cells[i];
var html    = cell.innerHTML;
var changed = html.replace(/([+-]?(\d|\.)+)/, function(x) {
    var color = (+x) < 0 ? 'red' : 'green';
    return '<span style="color: ' + color + '">' + x + '</span>';
});

cell.innerHTML = changed;
}

HTML is like this:

<table id="tableID">
<thead>
<tr>
<th>Column heading 1</th>
<th>Column heading 2</th>
<th>Column heading 3</th>
<th>Column heading 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>348419 ( 26% )</td>
<td>99425 ( -1% )</td>
<td>946574 ( 0% )</td>
<td>99485 ( -11% )</td>
</tr>
<tr>
<td>33348419 ( 36% )</td>
<td>746733 (-32%)</td>
<td>36689279 (0)</td>
<td>27678 (28%)</td>
</tr>
<tr>
 <td>7734190 ( 6% )</td>
 <td>333879 ( 35% )</td>
 <td>3878 (-10% )</td>
 <td>4419 ( -99% )</td>
 </tr>
2
  • 1
    add the html markup plz Commented Jan 6, 2016 at 8:10
  • Try this: if(cell.indexOf('-') !== -1 ){ //greeen cell = cell.replace('this string', 'this green string'); }else { cell = cell.replace('this string', 'this red string'); } Commented Jan 6, 2016 at 8:44

3 Answers 3

1

The problem is with the regular expression you are using. To retrieve only the number in the parenthesis, use /\(\s*([+-]?(\d)+)(?:%\s*\))/. Then the function would look like this

var changed = html.replace(/\(\s*([+-]?(\d)+)(?:%\s*\))/, function(match, group) {
    var color = (+group) < 0 ? 'red' : 'green'
    return '<span style="color: ' + color + '">' + group + '</span>'
})
Sign up to request clarification or add additional context in comments.

3 Comments

this is giving me an error, saying that x is undefined
the color is changing as expected but it is removing the brackets and percentage sign. Any idea how to fix that pls
thanks mic4ael, I edited and this worked. Marking this an official answer.
1

The values are string. if you compare the string with numeric comparison operators, it will always be greater than 0. parse your html content in a way that it return only the 26 from "348419 ( 26% )" and -11 from "99425 ( -11% )"

in this code your regex not returning the valid value i.e.

"99425 ( -11% )".replace(/([+-]?(\d|\.)+)/, function(x) {console.log(x);})

output : x = 99425 which is always a positive integer.

2 Comments

parse your html content in a way that it return only the 26 from "348419 ( 26% ) And how would I do that, is the question.
Try this "348419 ( -26% )".replace(/[^\w\s]/gi, '').trim().split(' ').pop()
1

You need to escape your round brackets as otherwise they have a special purpose in regex: to group items. In addition, you need to allow for whitespace inside the brackets. So your expression becomes /\(\s*[+-]?[0-9.]+%\s*\)/ instead of /([+-]?(\d|\.)+)/

var cells = document.querySelectorAll('#tableID td');

for ( var i=0; i<cells.length; ++i ) {
    var cell    = cells[i]
    var html    = cell.innerHTML
    var changed = html.replace(/\(\s*[+-]?[0-9.]+%\s*\)/, function(x) {
        var color = (+x) < 0 ? 'red' : 'green'
        return '<span style="color: ' + color + '">' + x + '</span>'
    })

    cell.innerHTML = changed
}

3 Comments

@mic4ael thank you, I edited it and now it passes the tests on regex101.com
sure, but he is trying to retrieve only the value inside the parenthesis
the only problem that I'm getting is that values inside the parenthesis are in GREEN color irrespective of +ve or -ve values. Can it be fixed pls

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.