I am trying to update the color of specific keywords within a post.
For example:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam tempor lacinia urna eget gravida. Quisque magna nulla, fermentum fermentum od
#keyword1 #keyword2 #keyword3 #keyword4 #keyword5
If the above example is the content of the post, I want to change the styling of the keywords using Next.js.
function applyStylingToKeywords() {
var startIndices = [];
var endIndices = [];
for (let i = 0; i < post.body.length; i++) {
if (post.body[i] === "#") {
startIndices.push(i);
}
if (startIndices.length !== endIndices.length && post.body[i] == " ") {
endIndices.push(i);
}
}
for (let i = 0; i < startIndices.length; i++) {
const word = post.body.substring(startIndices[i], endIndices[i]);
const styledWord = word.style.color = "blue";
post.body.replace(word, styledWord);
}
return post.body;
}
I attempted the code above but encountered an error.
TypeError: Cannot set properties of undefined (setting 'color')