How can javascript replace a word in the text without using replace ?
var text = "Этот трактат по теории этики был очень популярен в эпоху Возрождения"; Here it is necessary to replace the word "theory" with "practice".
How can javascript replace a word in the text without using replace ?
var text = "Этот трактат по теории этики был очень популярен в эпоху Возрождения"; Here it is necessary to replace the word "theory" with "practice".
As a simple replacement for the replace method, you can use a couple of split and join methods.
In this form:
str.split('подстрока которую заменить').join('строка на которую заменить'); Example:
var text = "test 1, test 2, test 3, test 4, test 5, test 6"; document.body.innerHTML = text + '<br/>'+ text.split('test').join('newTest'); replace_all which is not in JavaScript (if without regexps). I recommend to add what is still different str.replace('a', 'b'); from str.split('a').join('b'); - tutankhamunFor variety, you can also substitute with indexOf() and substring() . More precisely, it is no longer "replacement" but "copy / cut / paste" , but it also works.
function replace(str, find, word) { var result = str, i = 0, len = find.length; while (len) { i = result.indexOf(find, i); if (i == -1) { break; } result = result.substring(0, i) + word + result.substring(i + len); i += len; } return result; } var newStr = replace("Этот трактат по теории этики", "по теории", "на практике"); console.log(newStr); // Этот трактат на практике этики indexOf saving the indexOf result :-) indexOf way, if you change it to the same value, then the cycle will never end :) - Grundywhile we can get a bonus. Completed :) - Alexander Igorevich Februarylen does not change the meaning of it while checking? - Grundyfind is empty, the loop will loop. It turns out: one rake removed, others put - tutankhamunSource: https://ru.stackoverflow.com/questions/908097/
All Articles
replace- Grundyreplace? why do you need it without him? - Grundysplit()/join()- Alexander Igorevich