HTML is very complicated to parse, so the best approach is to use a parser that already exists.
If you're doing this in a browser, you can use the one built into the browser: DOMParser.
If you're doing this in Node.js, there are several libraries to do it, such as jsdom. It provides an API almost identical to the one in web browsers.
Here's a jsdom example:
const dom = new JSDOM("<!doctype html>" + originalStr);
const doc = dom.window.document;
for (const childElement of doc.body.children) {
console.log(`${childElement.tagName} - ${childElement.textContent}`);
}
With your string, that would output:
FIRSTTAG - text inside first tag
SECONDTAG - 50
You'd write code using the DOM methods provided to create the output you're looking for. (Note the tag name normalization above; you may have to use nodeLocation to get the original capitalization if it matters to what you're doing.)