I want to find and replace strings in html and replace them as tags.
from my name is ron and his name is ben
to my name is <b>ron</b> and his name is <b>ben</b>
Here is my code, works partially: https://stackblitz.com/edit/react-gjriyz
export default function App() {
const arr = ["ron", "ben"];
const str = "my name is ron and his name is ben";
let res = "";
for (var i in arr) {
res = str.replace(arr[i], "<b>" + arr[i] + "</b>");
}
return (
<div className="App">
<span dangerouslySetInnerHTML={{ __html: res }} /> <br />
<br />
expected result: <br />
my name is <b>ron</b> and his name is <b>ben</b>
</div>
);
}