The regular expression I wrote works. But I don’t know how to get the results out of the array returned by exec().
Take this text:
<p>paragraph 1</p>
<p>paragraph 2</p>
<p>paragraph 3</p>
What I’d like to get back from the array returned by exec() is the following:
paragraph 1
paragraph 2
paragraph 3
My code looks like this:
var text = ‘<p>paragraph 1</p>’;
text += ‘<p>paragraph 2</p>’;
text += ‘<p>paragraph 3</p>’;
var re = /(?:<p[^>]*>)(.*?)(?:</p>)/gi;
var arrMatch = [];
var match;
while (match = re.exec(text)) {
arrMatch.push(match[0]);
}
My hope for the code is this:
1) Match but don’t capture <p> and </p>. Capture whatever comes between them, however. Each capture forms a group
2) Loop through matches and grab the 0th group (the stuff between the paragraph tags). In this case, there would be three matches, each having a group of one member (the 0th group).
However, what I’m getting back doesn’t give me this at all. I’m not really sure what it gives back, actually. The 0th group sometimes seems to be the entire match including paragraph tags, and sometimes it’s something else.
I can’t seem to formulate a Google search that would give me back an example of how to proceed.
Is there any concept of capture groups or match collections in JavaScript? Can someone help?!
Thanks.
–Brent