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
Post a Comment