This question has already been answered:
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>