UPDATED
The div that I will be adding after every 2nd word.
var some_div = '<div style="display:inline-block;color:red;">some_text</div>';
var text = $('#mydiv').text().match(/\w+/g);
Secondly, traverse through all the words and prefixed these words in the html of the div with a unique identifier text.
Here, I am add a string <index>$$ where <index> increments on each traversal.
var i = 1;
var count = 1;
var html = $('#mydiv').html();
text.forEach(function(word, index) {
var offset = html.indexOf(word);
while (html[offset - 1] == '$' && html[offset - 2] == '$') {
offset = html.indexOf(word, offset + 1);
}
if ((count % up_index) == 0) {
html = html.slice(0, offset) + (i++) + '$$' + html.slice(offset)
$('#mydiv').html(html);
}
count++;
});
Finally, loop through all the unique tokens and replace them with your html.
to find tokens use $('#mydiv').find(':contains(' + j + '$$)'); of jquery.
for (var j = 1; j < i; j++) {
var elm = $('#mydiv').find(':contains(' + j + '$$)');
if (elm.length == 0) {
console.log('inroot>>' + ':contains(' + j + '$$)');
var offset = $(':contains(' + j + '$$)').last().html().indexOf(j + '$$');
var t_html = $(':contains(' + j + '$$)').last().html().slice(0, (offset + (("" + j + '$$').length))).replace(/[0-9]\$\$/ig, '');
t_html += some_div;
t_html += $(':contains(' + j + '$$)').last().html().slice(offset + (("" + j + '$$').length));
$('#mydiv').html(t_html);
} else {
console.log('not inroot>>' + ':contains(' + j + '$$)');
$(some_div).insertAfter(elm.last());
}
}
Here is an Example where I have added div after every 2nd word
Firstly, I am fetching all the words inside the interested container as follows,
var some_div = '<div style="display:inline-block;color:red;">some text</div>';
var up_index = 2; // Word index that will be updated every 2nd word.
var text = $('#mydiv').text().match(/\w+/g);
var i = 1;
var count = 1;
var html = $('#mydiv').html();
text.forEach(function(word, index) {
var offset = html.indexOf(word);
while (html[offset - 1] == '$' && html[offset - 2] == '$') {
offset = html.indexOf(word, offset + 1);
}
if ((count % up_index) == 0) {
html = html.slice(0, offset) + (i++) + '$$' + html.slice(offset)
$('#mydiv').html(html);
}
count++;
});
for (var j = 1; j < i; j++) {
var elm = $('#mydiv').find(':contains(' + j + '$$)');
if (elm.length == 0) {
console.log('inroot>>' + ':contains(' + j + '$$)');
var offset = $(':contains(' + j + '$$)').last().html().indexOf(j + '$$');
var t_html = $(':contains(' + j + '$$)').last().html().slice(0, (offset + (("" + j + '$$').length))).replace(/[0-9]\$\$/ig, '');
t_html += some_div;
t_html += $(':contains(' + j + '$$)').last().html().slice(offset + (("" + j + '$$').length));
$('#mydiv').html(t_html);
} else {
console.log('not inroot>>' + ':contains(' + j + '$$)');
$(some_div).insertAfter(elm.last());
}
}
$('#mydiv').html($('#mydiv').html().replace(/[0-9]\$\$/ig, ''));
.highlight {
color: red;
display: inline-block;
}
b {
background-color: blue;
}
i {
background-color: yellow;
}
i,
b {
border: 1px solid green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="mydiv">
sample text
<b><a>sample text</a></b> <i>sample text </i>
......
<i>inner text </i> <i>sample text </i>
......
<b>sample text </b> <i>sample text </i>
</div>
<i>inner</i><div class="highlight">highlight text</div><i> text </i>? That way, a strict interval of 200 words between insertions could be enforced.