React
HTML
<div id="app"></div>
React
const content = {
"content": [
{
"text": "#number# Tips to Get #goal#",
"types": ["email"]
},
{
"text": "Your Search For #goal# Ends Here",
"types": ["ad", "email"]
}
]
}
const parameters = {
number: 'A NUMBER',
goal: 'A GOAL'
}
function safeReplace(string) {
return string.split(/(#\w+#)/g)
.filter(x => x.length > 0)
.map(x => {
const m = /#(\w+)#/.exec(x);
if (!m) return x;
const repl = parameters[m[1]];
if(repl) return <span style={{background:'yellow'}}>{repl}</span>;
return x;
})
}
function App(props) {
const listItems = content.content.map((result, index) => (
<li key={index}>
{safeReplace(result.text)}
</li>
));
return (<div>{listItems}</div>);
}
ReactDOM.render(<App />, document.querySelector("#app"))