How to prevent scroll from moving on text update?

I have a TMP_InputField with a vertical scrollbar and I’m changing the text of the field while the player types with the purpose of highlighting some words etc.

Here is some sample code:

public class NewBehaviourScript : MonoBehaviour
{
    TMP_InputField input;
 
    void Start()
    {
        input = GetComponent<TMP_InputField>();
    }
 
    void Update()
    {
        input.text = Regex.Replace(input.text, "00000", "11111");
    }
}

When I change something in the text, the scrollbar automatically positions itself so that the changed line is on the bottom of the input field (like in the GIF), which is annoying.

I’ve tried bringing back the scrollbar to the old position, but this did nothing:

float scroll = input.verticalScrollbar.value;
input.text = Regex.Replace(input.text, "00000", "11111");
input.verticalScrollbar.value = scroll;

As far I can tell when I do input.text = …, scroll value changes to 0 and then changes again based on the caret position. Is there any way to prevent this recalculation, or to at least bring back the old value?

You can save the value in a variable and reset it, like int tempCaretPos;