简体   繁体   English

JavaScript:如何使用 ReactDOMServer.renderToString 格式化 str.replace,使用捕获的元素作为道具

[英]JavaScript: How to format a str.replace with ReactDOMServer.renderToString, using a captured element as a prop

I am trying to apply this regex我正在尝试应用此正则表达式

str.replace(/<L href=(.*?)>(.*?)<\/L>/g, ReactDOMServer.renderToString(<L href={$1}>$1</L>))

But it doesn't recognize the $1 that is in the href={$1} (says it is not defined), while it does appear in the second place and it shows it is correctly captured但它不识别$1那就是在href={$1}说,这是没有定义),而它会出现在第二位,它显示了其被正确地捕获

How can I write it?我该怎么写?

*So the problem is not with the regex, but with how can I use the captured variable as a parameter form the component *所以问题不在于正则表达式,而在于如何使用捕获的变量作为组件的参数


Maybe something like this?也许是这样的?

($1, $2) => ReactDOMServer.renderToString(<L href={$1}>$1</L>)

Can be workarounded by not capturing the comas可以通过不捕获昏迷来解决

str.replace(/<L href="(.*?)">(.*?)<\/L>/g, ReactDOMServer.renderToString(<L href="$1">$1</L>))

But isn't there a better way?但是没有更好的方法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM