reactjs - How to parse a string to a React component? -


say have string const s = "i love $fb", , wanna get:

<p>i love <a href="/symbol/fb">$fb</a></p> <rendercontent content={s}>

i can use reg extract symbol, how can turn string react component, without using dangerouslysetinnerhtml?

an alternative render function component use regex capturing groups, no validation done on .exec result however:

render() {   const subs = /(^.*)(\$(.*))/.exec(this.props.content);   return (     <p>       { subs[1] }       <a href={`/symbol/${subs[3]}`}>{subs[2]}</a>     </p>   ); } 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -