I have created a function that substitutes any instances of words in an array with markdown links that lead to a glossary page.
For example:
const text = "This is an example text with an [example](example.com) markdown link.";
const highlightedWords = ["xxx", "example", "yyy"];
const replaceWords = (text: string, highlightedWords: string[]) => {
if (highlightedWords.length == 0 || text == null) {
return text;
}
return text.replace(
new RegExp(`\\b(${highlightedWords.join("|")})\\b`, "gi"),
`[$1](/glossary#$1)`
);
};
console.log(replaceWords(text, highlightedWords));
// Output: This is an [example](/glossary#example) text with an [[example](/glossary#example)]([example](/glossary#example).com) markdown link.
I am facing an issue where the regex function does not ignore text fragments that are already enclosed within markdown link syntax. I would like it to do so.
Your assistance on this matter would be greatly appreciated! Thank you!