【发布时间】:2017-08-26 08:07:36
【问题描述】:
我正在使用正则表达式来使用双下划线来加粗字符。这是我的正则表达式,它有效:
"I'm making __this__ bold".replace(/\__([^*]+)\__/g,"<b>$1</b>");
这给了我这个:
"I'm making <b>this</b> bold"
但是我如何让它与 React 一起工作呢?我有以下代码,但不起作用:
{cars.map(car => {
return (
<div>
<p>{car.description.replace(/\__([^*]+)\__/g,"<b>$1</b>")}</p>
</div>
)})}
在这种情况下,{car.description} 例如:
"The __Audi RS 6 quattro__, commonly referred to as __the RS6__"
我希望它显示为:
“Audi RS 6 quattro,通常称为RS6”
编辑:
有了这个
<p>{car.description.replace(/\__([^*]+)\__/g,"<b>$1</b>")}</p>
我得到一个字符串:
"The <b>Audi RS 6 quattro</b>, ..."
但是如何让 JSX 识别以将其显示为粗体,而不是前端的
标签?
【问题讨论】:
-
请试试这个,
<p>{car.description.replace(/\__([^*]+)\__/g,"<b>$1</b>")}</p>
标签: javascript regex reactjs