Last position of a string

Asked

Viewed 860 times

2

I have the following code:

 var elements = document.getElementsByTagName('*');
for (var i = 0; i < elements.length; i++) {
     var txt = elements[i].innerHTML.replace('Estoque1', 'Estoque Vencido');
    }

It captures all page tags, and changes the word Stock to Expired Stock. I need to make a code that it erase anything below the word "Stock Past", after changing the word. In Case, if it were in Danish I would use the lastpos, now in Javascript I have no idea, someone can help me ?

2 answers

2

You could use the function .slice() Javascript, then your code would look like this:

var elements = document.querySelectorAll('body *');
 for (var i = 0; i < elements.length; i++){
     var txt = elements[i].innerHTML;

     //Só dispara as alterações caso o conteúdo contenha a palavra "Estoque1"
     if(txt.indexOf('Estoque1') > -1){
         txt = txt.replace('Estoque1', 'Estoque Vencido');
         txt = txt.slice(0,15);
         elements[i].innerHTML = txt;
     }
 }

I also subistitui .getElementsByTagName() for .querySelectorAll() for better functioning.

Example: FIDDLE

  • in this case that you posted, it changes all the words Stock 1 that exist on the page, I need different. After he finds the word Stock 1, he switches it for Expired Stock, and whatever is AFTER the word expired stock he erases everything from the page, understood ?

  • But this is what he does, only relative to the element in which the word is found. Or you want it to delete all other elements in the DOM below the word "Stock1"?

  • I want it to all elements, below the word Stock 1, all.... that you have in html.

1


With the use of regular expressions one can do this:

var txt = "Foo Bar Estoque1 Baz Poo Par Paz...";
txt = txt.replace(/Estoque1(.*)/, "Estoque Vencido");
// => Foo Bar Estoque Vencido

The function laspos(or LastDelimiter) may be considered equivalent to lastIndexOf() javascript.

var elementos = document.querySelectorAll('body *');
for (var indice = 0; indice < elementos.length; indice++)
{
   var txt = elementos[indice].innerHTML;
   var last = txt.lastIndexOf('Estoque1'); 
   if(txt.indexOf('Estoque1') > -1)
   {
      txt = txt.substring(0, last) + "Estoque Vencido" + txt.substring(last + txt.length);
      elementos[indice].innerHTML = txt;
   }
}

Fiddle

  • in his case, he erased the entire page, and left only the word Expired Stock appearing. I need him to erase everything to "DOWN" or "ONLY AFTER" the expired stock, over it he leaves everything normal, you know ?

  • Thanks as always for the attention @Sunstreaker, tell me one thing, as it would be a full page in HTML, and I do not know if it will be inside a div tag, body etc.... Is there any way I can do it using FOR ? so he can search the entire page ? example: var Elements = Document.getelementsbytagname('*'); for (var i = 0; i < Elements.length; i++) ? thus ?

  • perfect script, worked out.... Thanks friend...

  • If you can clarify me still in this topic, after I delete the given word down, how do I insert an image of that word down ? is possible ? I know that in Delphi I would use the Insert command.

  • @user7605 I believe it is possible yes, see this question. Just adapt it.

  • Thank you @Sunstreaker!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.