找到你要的答案

Q:how can i replace the string between the two indexes

Q:如何替换两个索引之间的字符串

So i have a string suppose it is india is great india i want to replace india but only one . suppose is is last one with other character . I have done something which is not working perfectly. issue with this is that it is replacing first letter and secod time it will replace other .

actually i have a text area and i want to delete the whole word from specific position . here is the example JSfiddle. If i remove any tag like <companyname>

my js code to delete on backspace is

if(e.keyCode==8)
             {
                 var strval=tb.value;
                  var dlted_tag = (strval).slice(thisMatchStart+1,thisMatchEnd);
                  tb.value=strval.replace(dlted_tag,"");
             }

I also tried this

if(e.keyCode==8)
             {
                 var strval=tb.value;
                  var dlted_tag = (strval).slice(thisMatchStart+1,thisMatchEnd);
                  tb.value=strval.replaceAt(thisMatchStart,"");
             }

replace at function is

String.prototype.replaceAt=function(index, character) {
    return this.substr(0, index) + character + this.substr(index+character.length);
}

nothing happening . No error

所以我有一个字符串假设是印度是伟大的印度我想取代印度,但只有一个。假设是最后一个与其他字符。我做了一些不完美的工作。这个问题是更换第一封信,二次将取代其他。

实际上我有一个文本区域,我想从特定的位置删除整个单词。这里是JSfiddle。如果我删除任何标签如<;公司名称& gt;

我的js代码,删除键是

if(e.keyCode==8)
             {
                 var strval=tb.value;
                  var dlted_tag = (strval).slice(thisMatchStart+1,thisMatchEnd);
                  tb.value=strval.replace(dlted_tag,"");
             }

我也试过这个

if(e.keyCode==8)
             {
                 var strval=tb.value;
                  var dlted_tag = (strval).slice(thisMatchStart+1,thisMatchEnd);
                  tb.value=strval.replaceAt(thisMatchStart,"");
             }

在功能上替换

String.prototype.replaceAt=function(index, character) {
    return this.substr(0, index) + character + this.substr(index+character.length);
}

什么也没发生。没有错误

answer1: 回答1:

Try:

 tb.value = strval.slice(0, thisMatchStart) + strval.slice(thisMatchEnd);

尝试:

 tb.value = strval.slice(0, thisMatchStart) + strval.slice(thisMatchEnd);
javascript  jquery