Regex Not Capturing Multiple Occurrences of HTML Tags in JavaScript
I'm trying to extract all occurrences of specific HTML tags (like `<div>` and `<span>`) from a block of HTML using a regex in JavaScript. My current regex seems to only capture the first occurrence of the tags, which is not what I intended. Hereโs the regex Iโm using: ```javascript const htmlString = '<div>Hello</div><span>World</span><div>Another div</div>'; const regex = /<div>(.*?)</div>|<span>(.*?)</span>/g; const matches = htmlString.match(regex); console.log(matches); ``` When I run this code, I only get an array containing the first `<div>` and the first `<span>`: ``` [ '<div>Hello</div>', '<span>World</span>' ] ``` I expected it to give me all `<div>` and `<span>` elements, like: ``` [ '<div>Hello</div>', '<span>World</span>', '<div>Another div</div>' ] ``` Iโve tried modifying the regex to include global flags and lookaheads, but Iโm still exploring. Iโm working with Node.js v16.0.0, so I thought regex would handle this properly, but it seems like I'm missing something essential. Any suggestions on how I can adjust my regex to capture all occurrences?