javascript remove last word in textarea - Stack Overflow

Can anyone help me to remove last word from the text area and it will replace with some another word.Ex

Can anyone help me to remove last word from the text area and it will replace with some another word.

Example:

I like my dog

should bee

I like my cat

The last word is not always dog.

I'll update my code here

    function KeyCheck(e) {

var KeyID = (window.event) ? event.keyCode : e.keyCode;

switch(KeyID) {
    case 32:
        text2 = document.form1.box1.value;
        text2 = ReplaceLastWord(text2, "cat");
                alert(text2);
    }
}

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

I have put alert to check whether it's replaced or not

Can anyone help me to remove last word from the text area and it will replace with some another word.

Example:

I like my dog

should bee

I like my cat

The last word is not always dog.

I'll update my code here

    function KeyCheck(e) {

var KeyID = (window.event) ? event.keyCode : e.keyCode;

switch(KeyID) {
    case 32:
        text2 = document.form1.box1.value;
        text2 = ReplaceLastWord(text2, "cat");
                alert(text2);
    }
}

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

I have put alert to check whether it's replaced or not

Share Improve this question edited Sep 5, 2013 at 13:56 user1228 asked Nov 13, 2011 at 4:24 Jonny KingJonny King 952 silver badges11 bronze badges 6
  • 1 Is your problem how to access the contents of the textarea, or how to figure out what the last word is? Is it always the last word, or any use of the word? What's your expected output if the text is "Do you like my dog? I like my dog." – nnnnnn Commented Nov 13, 2011 at 4:32
  • 1 @nnnnnn He may need someone to like his textarea's dog... – Exception Commented Nov 13, 2011 at 4:40
  • 3 The dog ate my textarea. – Dave Newton Commented Nov 13, 2011 at 4:45
  • @nnnnnn - Do you like my dog? I like my cat – Jonny King Commented Nov 13, 2011 at 5:00
  • @jonny-king I think you have got answer.. Your cat will be with you only to like.. You did not post any HTML or code except i like my dog and cat.. Please have a look at answers for your question and try to find whether it matches your requirement or not. Else try to post some HTML except cat and dog.. – Exception Commented Nov 13, 2011 at 5:03
 |  Show 1 more ment

2 Answers 2

Reset to default 6
var str = 'I like my dog'; 
var newEndStr = 'cat'; 

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

console.log(ReplaceLastWord(str, newEndStr));

output:

I like my cat

You can search for the last space character and replace the text after it:

text = text.replace(/\w+$/, '');
text = text.substring(0, text.lastIndexOf(' ')) + ' foobar';

The first line strips all trailing spaces.


You can bind a onkeydown event to your textarea. If e.keyCode || e.which is equal to the space bar code (no idea what it is), pass the contents of the textarea through this function:

function getLastWord(text) {
  temp = text.replace(/\w+$/, '');

  return temp.substring(temp.lastIndexOf(' '), temp.length - 1)
}

Then check if the last word is dog. Now you can replace the last word.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744404900a4572581.html

相关推荐

  • javascript remove last word in textarea - Stack Overflow

    Can anyone help me to remove last word from the text area and it will replace with some another word.Ex

    7天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信