I have string like: I agree with the <link>Privacy Policy</link> & <link>Terms of Use</link>
;
I am trying to convert it into
I agree with the <Button onClick={..}>Privacy Policy</Button onClick={..}> & <Button>Terms of Use</Button>
;
Expected Result:
What I have tried so far
After going through various resources on the internet I have implemented this method:
const updateLinks = ({content, component: Component}) => {
const linkTexts = content.match(/<link>(.*?)<\/link>/g);
const updatedLinkTexts = linkTexts.map(text => {
const innerText = /<link>(.*?)<\/link>/g.exec(text);
return <Button onClick={() => console.log("working")}>{innerText[1]}</Button>;
});
// Logic for non-link content
return updatedLinkTexts;
}
Attempt Result:
I can also add the some messy lines of code in order to add the remaining non-link content. But I am looking for some more elegant solutions otherwise, I can use the last option of some more messy lines of code.
Note: Use of regex is optional. Non-regex solutions are also cool.
Feel free to comment down for clarifications, if needed.