为了账号安全,请及时绑定邮箱和手机立即绑定

从字符串中解析标签并以 JSX 形式返回

从字符串中解析标签并以 JSX 形式返回

交互式爱情 2023-08-24 17:58:49
所以我的后端返回一个带有两组强标签的字符串。好像:const string = "<strong>Name N.</strong> How are you doing today? <strong>more text</strong>"由于我使用的是 React Native,因此无法按原样显示。我想返回看起来像这样的东西:<Text>  <Text style={{fontWeight: 'bold'}>Name N.</Text>  How are you doing today?  <Text style={{fontWeight: 'bold'}>more text</Text><Text>解决这个问题的最佳方法是什么?
查看完整描述

1 回答

?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

你可以用两种方式做到这一点


第一个选项


通过网络视图


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);


查看完整回答
反对 回复 2023-08-24
  • 1 回答
  • 0 关注
  • 129 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信