I've got a text saved in a longer string and want to replace some words of the text with a highlighted version or a markdown link to a glossary page where the words are described. The words that should be filtered are stored in a string array.
At the moment my code looks something like this:
const text = "Lorem ipsum xxx dolor sit amet."
const highlightedWords = ["xxx", "yyy"]
const newText = text.replace(new RegExp(highlightedWords.join("|"), "gi"), "[same-word-here]('/glossary#same-word-here)');
The part with same-word-here should be the same word that was replaced before, but I don't know how to get this one in this position.
If you need a better known example: It should look like a linked article in a wikipedia text
Thanks for your help.
CodePudding user response:
Wrap the the expression in a capture group ()
, and then use $1
to use the capture group's content. You should also use \b
(word boundary) to prevent text replacement when it's in another work, for example "xxxzzz".
const text = "Lorem ipsum xxx dolor sit amet."
const highlightedWords = ["xxx", "yyy"]
const newText = text.replace(new RegExp(`\\b(${highlightedWords.join("|")})\\b`, "gi"), "[$1]('/glossary#same-word-here)'");
console.log(newText)
CodePudding user response:
If I understand your goal correctly, you don't need regex for this since you have an array of words to iterate through.
let text = 'Lorem ipsum xxx dolor sit yyy amet.';
const highlightedWords = ['xxx', 'yyy'];
for (word of highlightedWords) {
if (text.includes(word))
text = text.replace(word, `[${word}]('/glossary#${word})`);
}
console.log(newText);
// result: Lorem ipsum [xxx]('/glossary#xxx) dolor sit [yyy]('/glossary#yyy) amet.
Or use a new variable so you still have original if you need it
const text = 'Lorem ipsum xxx dolor sit yyy amet.';
const highlightedWords = ['xxx', 'yyy'];
let newText = text;
for (word of highlightedWords) {
if (newText.includes(word))
newText = newText.replace(word, `[${word}]('/glossary#${word})`);
}
console.log(newText);
// result: Lorem ipsum [xxx]('/glossary#xxx) dolor sit [yyy]('/glossary#yyy) amet.