Using .replace () at a specific index

Is there a function that can replace a row in a row once with a specific index? Example:

var string1="my text is my text"; var string2="my"; string1.replaceAt(string2,"your",10); 

and the final result will be "my text is your text", or:

 var string1="my text is my text"; var string2="my"; string1.replaceAt(string2,"your",0); 

and in this case the result will be "your text is my text."

+7
javascript jquery algorithm
source share
3 answers

if you do not know the index.

 var s = "Stack Overflow"; var index = s.indexOf('w'); s = s.substr(0, index) + 'x' + s.substr(index + 1); 
+1
source share
 function ReplaceAt(input, search, replace, start, end) { return input.slice(0, start) + input.slice(start, end).replace(search, replace) + input.slice(end); } 

jsfiddle here

PS. change the code to add empty checks, border checks, etc.

+1
source share

From the "related questions" panel, this old answer seems to be applicable to your case. Replacing a single character (in my mentioned question) is not much different than replacing a string.

How to replace a character in a specific index in JavaScript?

0
source share

All Articles