From your comment, you can edit the question (just for clarity, without trying to be a jerk). In addition, splitting in space, counting has words. '\ s' is a space '\ n' - a new line '\ t' - carriage return, '\ r \ n' - a special type of carriage return. You could create a more complex regular expression to cover anything that is not a newline or space '\ t \\ s \ r \ n'. To put an image in a line, you can use a style trick, as described below.
If you want the image not to repeat, change:
function check() { if (count >= 20) { newHtml += '<img src="http://blog.leadlovers.com.br/wp-content/uploads/sites/23/2014/03/marca21-160x160.png" />' count = 0; } }
For even better matching, try align = "right", which wraps text around the image.
//jQuery(function ($) { jQuery( document ).ready(function( $ ) { var img = '<img align="right" src="http://blog.leadlovers.com.br/wp-content/uploads/sites/23/2014/03/marca21-160x160.png" />' $(".newsitem_text").html(function (i, h) { return h.replace(h.split(/\s+/).slice(20,21).join(' '), function (m) { return m + img; }); }); });
to
function check() { if (count == 20) { newHtml += '<img src="http://blog.leadlovers.com.br/wp-content/uploads/sites/23/2014/03/marca21-160x160.png" />'; } }
To avoid using an unpleasant loop, you can chop it in another place or use a splice:
//jQuery(function ($) { jQuery( document ).ready(function( $ ) { var img = '<img src="http://blog.leadlovers.com.br/wp-content/uploads/sites/23/2014/03/marca21-160x160.png" />' $(".newsitem_text").html(function (i, h) { return h.replace(h.split(/\s+/).slice(20, 21).join(' '), function (m) { return m + img; }); }); });
To wrap your image in words, use align = "left" or align = "right" in the image tag.
<img align="right" src="http://blog.leadlovers.com.br/wp-content/uploads/sites/23/2014/03/marca21-160x160.png" />