views:

65

answers:

3
<th>Prêmio</th>
    <td colspan="11">
    <div class="res"><img class="r1" src="img/x.gif" alt="Madeira" title="Madeira" />215 | <img class="r2" src="img/x.gif" alt="Barro" title="Barro" />193 | <img class="r3" src="img/x.gif" alt="Ferro" title="Ferro" />192 | <img class="r4" src="img/x.gif" alt="Cereal" title="Cereal" />202</div><div class="carry"><img class="car" src="img/x.gif" alt="carregamento" title="carregamento" />802/1800</div></td></tr></tbody></table><table cellpadding="1" cellspacing="1" class="defender">
    <thead>
    <tr>

i'm trying to get "802/1800", but it's driving me insane. if I use:

var myregexp = /title="carregamento"/;

it works

but going to the next step which is:

var myregexp = /title="carregamento" \/>/

already returs me null.

var myregexp = /title="carregamento" \/>/;

var match = myregexp.exec(document.documentElement.innerHTML);

FM_log(7,"match="+match);

if (match != null)
    resultado.push(match[1]);
+1  A: 

You should probably post the exact code, because there may be something slight going wrong that's not exactly having to do with the regex object.

If I test this on regextester.com, it works perfectly.

I use the following regex, and it matches the string up to 802/1800, and selects 802/1800 into a capture group.

title="carregamento" \/>(\d+\/\d+)
Rich
down there is the rest of the code related to it.
Fernando SBS
regextester.com works, but not if you use the correct sintax which is/title="carregamento" \/>(\d+\/\d+)/
Fernando SBS
the "/" characters that denote a string is a regex in javascript are not required. You only have to supply the actual regex string
Rich
if I try without the first / it returns me an error.
Fernando SBS
+1  A: 

The regexp you posted is correct:

var myregexp = /title="carregamento" \/>/

actually this one matches the string just before the "802/1800" string

Marco Demajo
the full code is not working. it's returning me as null.
Fernando SBS
A: 

Found what the problem was. Apparently there's a difference between what Firefox show me when I select "view document source" and what javascript is giving me as the source. Here's the difference:

firefox source:

<img class="car" src="img/x.gif" alt="carregamento" title="carregamento" />802/1800</div>

javascript source: (I created a LOG showing me document.documentElement.innerHTML

<img class="car" src="img/x.gif" alt="carregamento" title="carregamento">802/1800</div>

so the difference was a mere />

I also improved the code to:

        var myregexp = /title="carregamento">(.+?)\/(.+?)<\/div>/;


        FM_log(7,"myregexp="+myregexp);

        var resultado = [];

        var match = myregexp.exec(document.documentElement.innerHTML);

        //FM_log(7, document.documentElement.innerHTML);

        FM_log(7,"match="+match);

        if (match != null) {
            resultado.push(match[1])
            resultado.push(match[2])
            };

        FM_log(7,"resultado[0]="+resultado[0]+" resultado[1]="+resultado[1]);           

        efficiency = Math.round(resultado[0] / resultado[1] * 100);

        gain = resultado[0];

this is the final code and works perfectly.

Thanks for everyone who contributed.

Fernando SBS