JSFiddle - React, Tailwind, and code Playground
by Aleksey Karagodnikov
HTML
<div id="words">
<p>что-то там...</p>
<p>hello world</p>
<!--вот это выбираем--->
</div>
<div id="insert">
<p>text <span>1</span></p>
<p>text <span>2</span></p>
<p>text 3 <span>abracadabra<!-- вот сюда вставляем--></span></p>
</div>
JavaScript
var results = getElementByContent('#words', '', 'hello world');
if (results.length > 0) {
var target = getElementByContent('#insert', 'span', 'abracadabra');
if (target.length > 0) {
target.text(target.text().replace(new RegExp('abracadabra', 'g'), 'hello world'));
}
}
/**
* @text - искомая строка
* @haystack - селектор тэга-обертки
* @specific - селектор дочернего тега, по умолчанию '*', т.е. любой дочерний элемент
*/
function getElementByContent (haystack, specific, text) {
specific = (!specific || typeof specific !== 'string') ? '*' : specific;
return $(haystack).find('*').filter(specific + ':contains("'+text+'")');
}