Looking for a solution to fix a bug in my self-made autocomplete feature. Consider this scenario where I need to autocomplete the final word in an incomplete sentence:
let text = 'Hello there, I am her'
My current method involves the user pressing ctrl
+ enter
to autocomplete with a suggested word displayed on the page. Let's assume the suggestion here is 'here'. Additionally, my controller tracks the insertion cursor position (index).
If I use the replace
function like so:
text.replace(word, suggestion);
(In this case, 'word' is 'her' and 'suggestion' is 'here'), it will replace the first occurrence of 'her'. The challenge lies in replacing a specific index within the text
string as there can be multiple instances of the word throughout. Is there a more efficient way to achieve this rather than using cumbersome if
conditions?
(For context, I am implementing this functionality with Angular keydown/keyup events)
EDIT>>>>> This query differs from the linked question since the problem at hand involves supporting users who may revisit previous sections of their sentence to autocomplete a new word.