The script changes the first line that coincides with the variable mx , and you need to replace the last matching line with the variable mx .
For the answer, thanks in advance.

 var str = document.getElementById('inp').value, mx = '200', dx = '900'; str = str.replace(mx, dx); document.getElementById('out').value = str; 
 <textarea rows="5" cols="10" id="inp">200 500 200 400</textarea> <textarea rows="5" cols="10" id="out"></textarea> 

Reported as a duplicate at Grundy. javascript Apr 28 '18 at 6:15 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    2 answers 2

     var str = document.getElementById('inp').value, mx = '200', dx = '900'; str = str.split("\n").reverse().join("\n"). replace(mx, dx). split("\n").reverse().join("\n"); document.getElementById('out').value = str; 
     <textarea rows="5" cols="10" id="inp">200 500 200 400</textarea> <textarea rows="5" cols="10" id="out"></textarea> 

    • Thanks, earned :) - Maxim Belov

    You can regular:

     str = str.replace(new RegExp(mx + '$'), dx); 

    Just keep in mind that in lines that are more complex than 200 , it may be necessary to escape the characters for regularizing characters.