- I have a string called
I want to order cheese chicken pizza.. - I want to replace
cheesewith<span style={{color: 'red'}}>cheese</span>and chicken with<span style={{color: 'green'}}>chicken</span>. - So I expect a result like
I want to order <span style={{color: 'red'}}>cheese</span> <span style={{color: 'green'}}>chicken</span> pizza.
In an array I have data like let arr = ['cheese', 'chicken']. I took the below approach:-
let text = "I want to order cheese chicken pizza."
arr.map(a=>{
let findText = new RegExp(a, 'g');
text = text.replace(findText, (match)=>(<span>{match}</span>))
});
return text;
I get the result I want to order [object object] [object object] pizza.
I checked maximum solution is for replacing single same string.
I want to achieve this because I will add eventListener to each span tag to get the details.
Can anyone help me out. Thnaks :)