I am using pure JavaScript and I want to get the last word where my current caret position is from the text string in a variable as shown in the below image, I want to get the unknown
word.
In C#, I used the below code concept and its working there perfectly...
private static char[] splitters = new char[] { ' ', '\n', '۔' };
int caretPosition = string.SelectionStart;
string tempLastWord = string.Substring(0, caretPosition);
string lastWord = tempLastWord.Substring(tempLastWord.LastIndexOfAny(splitters) + 1);
Now I want to do the same in the pure JavaScript as below...
var splitters = [ ' ', '\n', '۔' ];
var caretPosition = document.getElementById(Desired_ID).value.slice(0, document.getElementById(Desired_ID).selectionStart).length;
var tempLastWord = document.getElementById(Desired_ID).value.substring(0, caretPosition);
var lastWord = tempLastWord.substring(tempLastWord.lastIndexOf(splitters) + 1);
But my JavaScript code is not working perfectly. Can anyone help me...???
var lastWord = tempLastWord.substring(tempLastWord.lastIndexOf('\n') + 1);
lastWord = lastWord.substring(lastWord.lastIndexOf(' ') + 1);
lastWord = lastWord.substring(lastWord.lastIndexOf('?') + 1);
This works.
var lastWord = tempLastWord.substring(tempLastWord.lastIndexOf(splitters + 1));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.