-1
I am making an ajax request that will return me an "X" page. My goal is to cut the page and manipulate the outerHTML
to return only the <tr>
which correspond to a specific Pattern/regex.
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var body = this.responseXML.body.outerHTML;
var main = body.slice(body.indexOf("<main"), body.indexOf("</main>"));
var regex = /<tr[^>]*>(?:(?!<|AQUI)[\s\S])*(?:<(?!\/?tr)[^>]*>(?:(?!<|AQUI)[\s\S])*)*AQUI[\s\S]*?<\/tr>/gi;
if(regex.test(main)) {
var matches = main.match(regex);
$('#resposta').html('<p class="result">Os ramais relacionados à <strong>' + setor + '</strong> são:');
for(i = 0; i < matches.length; i++){
$('#resposta').append(matches[i]);
$('#resposta').append('<p class="more"><a href="intranet/ramais">Para mais detalhes acesse os Ramais do TJPB</a></p>');
}
} else {
$('#resposta').html('Nenhuma combinação encontrada');
}
}
};
xmlhttp.open("GET",url, true);
xmlhttp.responseType = "document";
xmlhttp.send();
The pattern is working perfectly, only it’s static. The point is, I can’t manipulate it, change it:
/<tr[^>]*>(?:(?!<|AQUI)[\s\S])*(?:<(?!\/?tr)[^>]*>(?:(?!<|AQUI)[\s\S])*)*AQUI[\s\S]*?<\/tr>/gi
Where has AQUI
would like to change the value dynamically according to a parameter. Let’s assume that I need to change to "abcd" or "efgh", for example.
ps. the url variable is previously populated and returns the page url
You can use the object
new RegExp(textoExpReg, 'gi')
, with the regular expression string passed to it being manipulated dynamically.– Benilson
Obg by suggestion. I was already on that track, but what really solved was the attention to the escape of the Pattern to the bars etc. Obg
– Joao Paulo Fechine Sette