javascript - 从字符串中解析标签并以 JSX 形式返回
问题描述
所以我的后端返回一个带有 2 组强标签的字符串。好像:
const string = "<strong>Name N.</strong> How are you doing today? <strong>more text</strong>"
由于我使用的是本机反应,因此无法按原样显示。我想返回如下所示的内容:
<Text>
<Text style={{fontWeight: 'bold'}>Name N.</Text>
How are you doing today?
<Text style={{fontWeight: 'bold'}>more text</Text>
<Text>
解决此问题的最佳方法是什么?
谢谢 :)
解决方案
你可以用两种方式做到这一点
第一个选项
通过网页浏览
const string = "<strong>Name N.</strong> How are you doing today? <strong>more text</strong>"
<WebView source={{html: string}} />
第二个选项
通过替换和拆分
const text = "<strong>Name N.</strong> How are you doing today? <strong>more text</strong>";
const [first, second, third] = text.replace(/(<\/strong>|<strong>)/g, '|').split('|').filter(cur => cur).map(cur => cur.trim());
console.log(first);
console.log(second);
console.log(third);
推荐阅读
- kubernetes - Access web server inside a pod in a vm from outside the VM
- c# - In ASP.net, what kind of IF statement could I use to hide a div if the image inside it matches the current page URL?
- android - Limiting the number of loaded items in memory with paging library
- cassandra - system.log 中的墓碑扫描
- ios - UITableViewCell detailTextLabel white space
- excel - Change tab colour based on cell value, runtime error
- python - cmd 和 bat 行为不同
- apache-spark - DynamicFrame 解决 Array 和 Struct 之间的选择
- imagemagick - 在png透明度内设置图像
- android - 将人脸匹配图像发送到 Android 上的 AWS Rekognition 时出错