Textmesh Pro Input Field Rich Text

I have a Textmesh pro Input field, that is using rich text tags. However, when the user edits the input, the tag doesn´t get removed but stays there.
Do you have any advice?
Thanks in advance

I don’t know if this is what you are looking for:

Don’t use HTML mark-up in input fields (for the very reason you just experienced). If you want bold, assign a bold font to the field.

You can overload the TMP_InputField Class, its not perfect but here is a caret based solution to get you going in the right direction. This solution fails if the user types a tag and is either backspace or delete focused, depending on which of the ending if block logic statements is constrained by lastShiftRight.

EDIT: Updated so it works with both delete and backspace keys as normal, users can still mess it up by typing in their own tag. This probably needs another layer of detection to wrap with and ignore for the string search.

 public class TagInputField : TMP_InputField {
    
    private int lastCaretPos = 0;
    
        private bool insideTag = false;
        private bool infrontTag = false;
        private bool behindTag = false;
        private bool ranFrame = false;
    
        private int lastOpen = 0;
        private int lastClose = 0;
        private int nextOpen = 0;
        private int nextClose = 0;
    
        private bool lastShiftRight = false;
    
    private void OnGUI()
{
    if (isFocused) //&& Input.anyKey
    {
        try
        {
            lastOpen = text.LastIndexOf('<', selectionStringAnchorPosition - 1);
            lastClose = text.LastIndexOf('>', selectionStringAnchorPosition - 1);
            nextOpen = text.IndexOf('<', selectionStringAnchorPosition);
            nextClose = text.IndexOf('>', selectionStringAnchorPosition);
        }
        catch (System.ArgumentOutOfRangeException)
        {
            Debug.Log("end of text field");
        }

        if (lastOpen > lastClose || (nextClose < nextOpen && nextOpen != -1)) insideTag = true;
        else insideTag = false;

        if ((nextOpen - 1) == selectionStringAnchorPosition) behindTag = true;
        else behindTag = false;

        if ((lastClose + 1) == selectionStringAnchorPosition) infrontTag = true;
        else infrontTag = false;

        if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow))
        {
            if (insideTag) caretPosition += 1;
        }
        else if (Input.GetKey(KeyCode.LeftArrow))
        {
            lastShiftRight = false;
        }
        else if (Input.GetKey(KeyCode.RightArrow))
        {
            lastShiftRight = true;
        }

        if (Input.GetKey(KeyCode.Delete))
        {
            if (insideTag)
            {
                DisableKey(KeyCode.Delete);
                selectionStringAnchorPosition = nextClose + 1;
                selectionStringFocusPosition = nextClose + 1;
            }
            lastShiftRight = true;
            //DisableKey(KeyCode.Delete);
        }
        if (Input.GetKey(KeyCode.Backspace))
        {
            if (insideTag)
            {
                DisableKey(KeyCode.Backspace);
                selectionStringAnchorPosition = lastOpen - 1;
                selectionStringFocusPosition = lastOpen - 1;
            }
            if (infrontTag)
            {
                DisableKey(KeyCode.Backspace);
                selectionStringAnchorPosition = lastOpen;
                selectionStringFocusPosition = lastOpen;
            }
            lastShiftRight = false;
        }

        

        if (infrontTag && !lastShiftRight) // && !Input.GetKey(KeyCode.RightArrow)
        {
            selectionStringAnchorPosition = lastOpen;
            selectionStringFocusPosition = lastOpen;
        }
        else if (insideTag && lastShiftRight)
        {
            selectionStringAnchorPosition = nextClose + 1;
            selectionStringFocusPosition = nextClose + 1;
        }
        else { lastShiftRight = false; }
    }
}
    
    static void DisableKeys(KeyCode[] keys)
        {
            if (!Event.current.isKey)
            {
                return;
            }
    
            foreach (KeyCode key in keys)
            {
                if (Event.current.keyCode == key)
                {
                    Event.current.Use();
                }
            }
        }
    
        static void DisableKey(KeyCode key)
        {
            DisableKeys(new KeyCode[] { key });
        }
}