You can do it by breaking the string texto in array by tags </p> and then reassembling the texto with For Each adding the tags again </p> and when you arrive at the third loop lap, add what you want after the third tag </p>. Just use a variable that starts from 0 and every turn of the For Each increase +1. When the value of that variable is 2 (means the loop is on the 3rd lap), add into a variable that has the tag </p> the content you want to insert after it, in case the Adsense code.
Would look like this:
<%
texto="<p>primeiro paragrafo.</p><h2>titulo 1</h2><p>segundo paragrafo.</p><h2>titulo 2</h2><p>terceiro paragrafo.</p> <p>primeiro paragrafo.</p><h2>titulo 1</h2><p>segundo paragrafo.</p><h2>titulo 2</h2><p>terceiro paragrafo.</p> <p>primeiro paragrafo.</p><h2>titulo 1</h2><p>segundo paragrafo.</p><h2>titulo 2</h2>"
texto2 = split(texto, "</p>") ' quebra em array
texto = "" ' esvazia a variável
indice = 0 ' cria a variável
For Each item In texto2
if indice = 2 then
fecha_p = "</p><script async src=""//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js""></script>"&_
"<ins class=""adsbygoogle"""&_
" style=""display:inline-block;width:300px;height:250px"""&_
" data-ad-client=""ca-pub-3438726300391994"""&_
" data-ad-slot=""5174009669""></ins>"&_
"<script>"&_
"(adsbygoogle = window.adsbygoogle || []).push({});"&_
"</script>"
else
fecha_p = "</p>"
end if
texto = texto & item & fecha_p ' concatena as strings
indice = indice + 1 ' incrementa a variável
Next
%>
The result of the variable texto will look like this:
<p>primeiro paragrafo.</p><h2>titulo 1</h2><p>segundo paragrafo.</p><h2>titulo 2</h2><p>terceiro paragrafo.</p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><ins class="adsbygoogle" style="display:inline-block;width:300px;height:250px" data-ad-client="ca-pub-3438726300391994" data-ad-slot="5174009669"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script> <p>primeiro paragrafo.</p><h2>titulo 1</h2><p>segundo paragrafo.</p><h2>titulo 2</h2><p>terceiro paragrafo.</p> <p>primeiro paragrafo.</p><h2>titulo 1</h2><p>segundo paragrafo.</p><h2>titulo 2</h2></p>
Note that the Adsense code was positioned after the third </p>. If you want after the 4th, for example, just change the 2 in if indice = 2 then for 3.
Also note that you need to duplicate the double quotes of the attributes of the Adsense script to escape the double quotes that delimit the variable string fecha_p.
In the code above I put an Adsense my, do not forget to exchange for the values of your Adsense.
Javascript-enabled:
Search for the third paragraph within a div (paragraphs need to be within a parent div) and add the Adsense code after it:
<%
texto="<p>primeiro paragrafo.</p><h2>titulo 1</h2><p>segundo paragrafo.</p><h2>titulo 2</h2><p>terceiro paragrafo.</p> <p>primeiro paragrafo.</p><h2>titulo 1</h2><p>segundo paragrafo.</p><h2>titulo 2</h2><p>terceiro paragrafo.</p> <p>primeiro paragrafo.</p><h2>titulo 1</h2><p>segundo paragrafo.</p><h2>titulo 2</h2>"
%>
<div id="texto">
<%=texto%>
</div>
<script>
document.addEventListener("DOMContentLoaded", function(){
var p = document.querySelectorAll("#texto > p")[2];
var adsense = document.createRange().createContextualFragment('<scr'+'ipt async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"><\/script>'
+'<ins class="adsbygoogle"'
+' style="display:inline-block;width:300px;height:250px"'
+' data-ad-client="ca-pub-3438726300391994"'
+' data-ad-slot="5174009669"></ins>'
+'<scr'+'ipt>'
+'(adsbygoogle = window.adsbygoogle || []).push({});'
+'<\/script>');
document.getElementById("texto").insertBefore(adsense, p.nextSibling);
});
</script>
See that you need to escape the backslash from closing the tag </script> of Adsense and concatenate the word script: <scr'+'ipt. Otherwise Javascript will understand that you are putting one script inside another.
Only that the methods document.createRange().createContextualFragment() may not work in older browsers.
With jQuery:
It’s a little simpler. The dial $("#texto p:eq(2)") selects the third paragraph within #texto and the .after() inserts the HTML after it:
$(function(){
var adsense = '<scr'+'ipt async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"><\/script>'
+'<ins class="adsbygoogle"'
+' style="display:inline-block;width:300px;height:250px"'
+' data-ad-client="ca-pub-3438726300391994"'
+' data-ad-slot="5174009669"></ins>'
+'<scr'+'ipt>'
+'(adsbygoogle = window.adsbygoogle || []).push({});'
+'<\/script>';
$("#texto p:eq(2)").after(adsense);
});
Please read that answer
– danieltakeshi
So it is not possible to marry only the third </p>.
– Rod
The simplest way would be to use programming, i.e., use the expression to search for all
</p>and catch the third using programming logic.– KaduAmaral
Which programming language do you want to use? ASP Classic (Vb) or Javascript (front-end)?
– Guilherme Nascimento
Asp classic. Vb
– Rod
Face, what I’m really trying to do is this. take this </p> that is the end of the paragraph, in a current text and replace it with something else, right after this </p>, would be a banner in javscript. (I tried it in javscrit, but it would be a javscript inside another, without success). Only it would have to be in that third </p>, or fourth, my choice. I thought of regex.
– Rod
That is, I want to include a javascript (Adsense), right after the closing of the third </p>
– Rod
I thought in regex I could marry the same character, in the position I chose in the text.
– Rod
From what I understand you want to take the second
<p></p>and put after it an element that will stay before the third<p></p>right? It can be with jQuery?– hugocsl
can’t, because what I want to put on is also javscrit, an Adsense banner. I’ve even looked at this code here that wouldn’t work with one script inside another. https://answall.com/questions/227387/como-inser-an%C3%bancios-a-cada-x-par%C3%a1graphs-of-text
– Rod
That’s what I want, but here is wordpress plugin. https://www.estudopratico.com.br/syncretismo-religious/ note that advertisements are displayed in the course of the text.
– Rod
From what I understand, it would be better to work with DOM manipulation. Plot on the screen (or on an artificial DOM) and add the desired subtree just before the third paragraph
– Jefferson Quesado