I am trying to split different parts of a string into new objects depending on whether there is HTML markup. This is what I have tried, I have also tried with split(), however, I can't find a way to pull the content within the HTML markups.
const example = 'The lucky stars is an astrology themed <a href="/game/">game</a> and it consists of <strong>thousands of.</strong>'
const richTexts = [];
example.match(/\<.*?\>/g).forEach((e) => {
if (e.includes('<a')) {
richTexts.push({
type: 'hyperlink',
content: ''
})
} else if (e.includes('<strong') {
richTexts.push({
type: 'bold',
content: ''
})
} else {
richTexts.push({
type: 'text',
content: ''
})
}
})
console.log(richTexts);
Desired output is:
[
{
type: 'text',
content: 'The lucky stars is an astrology themed '
},
{
type: 'hyperlink',
content: 'game'
},
{
type: 'text',
content: ' and it consists of '
},
{
type: 'bold',
content: 'thousands of.'
}
]