0

i want to replace the string after @ character in the input field.

What i am trying to do? when i enter @ character in the input field the dropdown list with users is shown. when user selects one of the options from dropdown i want that value to be entered in the input field and the characters starting from @ should be replaced with the selected option from the dropdown.

For example say user types hello @u

dropdown is shown with options user1, user2, user3....

consider user selects user1 then the value in input field @u must be replaced with user1. so the complete string in input field must be

hello user1.

how do i replace @u with user1..

What i have tried?

i have tried to get the lastindex of the @ from the string using

string.lastIndexOf('@')

but not knowing how to replace the string starting from that index to end of string with the selected option.

Could someone help me solving this. thanks.

0

2 Answers 2

0

you can use slice function

var selectedusername = "user1";
var inputtext = "Hello @U";
inputtext = inputtext.slice(0, inputtext.lastIndexOf('@')+1);
var newinputtext = inputtext + selectedusername;

then replace the old input text with the new one

Sign up to request clarification or add additional context in comments.

1 Comment

this works but want to remove @ character as well so replacing this inputtext = inputtext.slice(0, inputtext.lastIndexOf('@')+1); with inputtext = inputtext.slice(0, inputtext.lastIndexOf('@')); works for me.
0

Have you tried the following?

var str = "kdkd@kdkdf" 
var textAfterSymbol = str.slice(str.indexOf("@") + 1)

textAfterSymbol would be "kdkdf" which you can then do whatever you please with it (without editing str might I add).

1 Comment

thanks this works too. except that i wanted the index of last occurence of @ in the string. upvoting this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.