[英]How to replace a string with another and with a different colour in ReactJS?
I have a string mapped from an array and I have used a regexp to return particular text.我有一个从数组映射的字符串,并且我使用了正则表达式来返回特定的文本。 I want this particular text to have a different color when it is rendered.
我希望此特定文本在呈现时具有不同的颜色。 However the current approach fails as it wont let me just set the style of the string in pure javascript.
然而,当前的方法失败了,因为它不会让我只在纯 javascript 中设置字符串的样式。 Is there a way around fixing this so the replace can work with the new text as a different colour.
有没有办法解决这个问题,以便替换可以使用不同颜色的新文本。
Ideally, the final outcome would look like this理想情况下,最终结果应该是这样的
@user This is text
Whereby @user
is blue and the rest is black其中
@user
为蓝色,其余为黑色
{this.state.dataReplies.map((n, i) => {
var re = /@(\S+)\b/g;
let oldstr = n.description.match(re);
console.log(oldstr);
let newstr = oldstr.style.colour = "#0066ff";
let str = n.description.replace(oldstr, newstr);
return (
<p>{str}</p>
);
})}
Thanks to @Jayce444 for the solution:感谢@Jayce444 的解决方案:
{this.state.dataReplies.map((n, i) => {
var re = /@(\S+)\b/g;
let oldstr = n.description.match(re);
let str = n.description.replace(oldstr, "");
return (
<p> <span style={{color: "#0066ff"}}>{oldstr}</span> {str}</p>
);
})}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.