I am trying to create a function which takes in 2 strings:
- title: "Lorem ipsum dolor sit amet",
- itle_highlighted: "ipsum amet",
and returns one string or array containing the all words in title
in addition words defined in the string title_highlighted
will have a certain property and replace their matching word in title
.
End result should be something like: <h1>Lorem<span style={{color: "orange"}}>impsum</span>dolor sit<span style={{color: "orange"}}>amet</span>/h1>
My approach so far:
const highlightHeading = (title, title_highlighted) => {
const title_arr = title.trim().split(/\s+/);
const title_highlighted_arr = title_highlighted.trim().split(/\s+/);
for (const element1 of title_arr) {
console.log(element1);
for (const element2 of title_highlighted_arr) {
if (element1 === element2) {
const highlighted = `<span style={{ color: "orange" }}>${element1}</span>`;
console.log(highlighted);
}
}
}
};
highlightHeading("Lorem ipsum dolor sit amet", "ipsum amet");
Thanks in advance.