Search code examples
javascripthtmlregexcapturing-group

What am I doing wrong with my regex?


I am trying to capture "Rio Grande Do Leste" from:

...
<h1>Rio Grande Do Leste<br />
...

using

var myregexp = /<h1>()<br/;

var nomeAldeiaDoAtaque = myregexp.exec(document);

what am I doing wrong?

update:

2 questions remain:

1) searching (document) didn´t produce any result, but changing it to (document.body.innerHTML) worked. Why is that?

2) I had to change it to: myregexp.exec(document.body.innerHTML)[1]; to get what I want, otherwise it would give me some result which includes <h1>. why is that?

3) (answered) why do I need to use ".*" ? I tought it would collect anything between ()?


Solution

  • Try /<h1>(.*?)<br/.