ReactJS渲染字符串与不间断空格 [英] ReactJS render string with non-breaking spaces

查看:605
本文介绍了ReactJS渲染字符串与不间断空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些支持字符串的道具,可以包含&这样的字符。
它也包含空格。我想用& nbsp; 来替换所有的空格。有没有一种简单的方法可以做到这一点?请记住,我不能仅使用此语法进行渲染:

 < div dangerouslySetInnerHTML = {{__ html:myValue}} /> gt ; 

因为我首先必须用它们的标记替换任何HTML实体。我不想这样做,它似乎太低级。



有没有办法我可以做到这一点?


<不使用& nbsp; HTML实体,您可以简单地使用Unicode非分隔空间字符:

 < div> {myValue.replace(/ / g,\\\ )}< / div> ; 


I have some props that has a string that could contain characters such as &. It also contains spaces. I want to replace all spaces with &nbsp;.

Is there an easy way I can do this? Bear in mind that I cannot just render using this syntax:

<div dangerouslySetInnerHTML={{__html: myValue}} />

because I would first have to replace any HTML entities with their markup. I don't want to have to do this, it seems too low level.

Is there a way I can do this?

解决方案

Instead of using the &nbsp; HTML entity, you can simply use the Unicode non-breaking space character:

<div>{myValue.replace(/ /g, "\u00a0")}</div>

这篇关于ReactJS渲染字符串与不间断空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆