3
What is the correct way to write a Regular Expression to find a paragraph?
// Expressão Regular
var minhaExpressao = /<p>.*</p>/;
How can I correct this variable?
3
What is the correct way to write a Regular Expression to find a paragraph?
// Expressão Regular
var minhaExpressao = /<p>.*</p>/;
How can I correct this variable?
3
You can use this expression to pick up the paragraph if it also has line breaks:
<p>[\w\W]+<\/p>
Example:
var p1 = document.querySelector("#div1").textContent;
var p2 = document.querySelector("#div2").textContent;
var re = /<p>[\w\W]+<\/p>/;
var para1 = p1.match(re)[0];
var para2 = p2.match(re)[0];
console.log(para1);
console.log(para2);
<textarea id="div1">
<p>
Isto é um parágrafo onde as tags não estão na mesma linha do texto
</p>
</textarea>
<textarea id="div2">
<p>Isto é um parágrafo onde as tags estão na mesma linha do texto</p>
</textarea>
\w\W -> captura letra e o que não é letra (ou seja, tudo)
+ -> quantificador que une as ocorrências
See on Regexr
2
You can use Regexp /^<p>.+<\/p>$/gm
, whereas:
^<p>
will ensure that the start of the line is <p>
.+
take all the elements after the initial tag, you can change to how to better understand this part, for only characters, only numbers, in this case it is for any character<\/p>$
to ensure that the end is </p>
remembering to escape the bar1
The only thing that is incorrect in your regex is </p>
, the bar should be escaped, getting like this: /<p>.*<\/p>/
;
I noticed that no one used a concept in regex called rearview mirror, that is, you get the reference of a particular group by the order in which it was defined in the expression.
The regex below will take any kind of tag, generalizing a little, but if you want specific paragraphs just change where you have (.*>)
for (p>)
, this regex will be able to catch the opening tag up to the closing tag, including the content between the tags.
<(.*>).*<\/\1
let texto = `<p>Este é uma paragrafo</p>
<b>Este é um texto em negrito</b>
<b>Fechamento Incorreto, não deve dar match<b>`
const expressao = /<(.*>).*<\/\1/gm
console.log(texto.match(expressao));
Browser other questions tagged javascript regex
You are not signed in. Login or sign up in order to post.