1

I need to replace last word from input value

Im using this code,

Get_value =$(input).val();
new_word= "new word";

function ReplaceLastWord(Get_value, new_word) {
 return Get_value.replace(/\w*$/, new_word);
}

$(input).val(ReplaceLastWord(Get_value, new_word));

Its working, but only for english... i am using tamil laguage in input

$(input).val()="கோச்மேடிக்ஸ் ச்னக்க்ஸ்";

For this language i need use this code.

Ashwin Balamohan
  • 3,303
  • 2
  • 25
  • 47
Dhamu
  • 1,694
  • 5
  • 22
  • 47

2 Answers2

3

try this

     var lastWord = function(inputString,newstring) {
     inputString=inputString.replace((""+inputString).replace(/[\s-]+$/,'').split(/[\s-]/).pop(),newstring)
     alert(inputString);
};

lastWord("கோச்மேடிக்ஸ் ச்னக்க்ஸ்",'hi');

​ ​

Live Demo

here inputString is input value and and newstring is word to replace with.

if this will help you mark this as answer

rahul
  • 7,573
  • 7
  • 39
  • 53
  • I cant understand this code, how to implement... which is input and which variable gives output from above program? can you please sent the program clearly – Dhamu Aug 31 '12 at 10:53
  • @Dhamu1 i have updated the code and attach demo also please have a look now – rahul Aug 31 '12 at 10:58
  • This both are only removing the last word, i need to replace the last word with new word. can you please make this? – Dhamu Aug 31 '12 at 11:11
  • i need to replace the last word with **new word**.Example : கோச்மேடிக்ஸ் ச்னக்க் to => கோச்மேடிக்ஸ் Get – Dhamu Aug 31 '12 at 11:15
0
function ReplaceLastWord(Get_value, new_word) {
 return Get_value.substring(Get_value.lastIndexOf(" ") + 1) + new_word;
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175