FernandoSBS
FernandoSBS

Reputation: 655

Javascript REGEX

<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]);

Upvotes: 0

Views: 123

Answers (3)

FernandoSBS
FernandoSBS

Reputation: 655

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.

Upvotes: 0

Marco Demaio
Marco Demaio

Reputation: 34437

The regexp you posted is correct:

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

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

Upvotes: 1

Rich
Rich

Reputation: 36866

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+)

Upvotes: 1

Related Questions