我正在调用返回HTML的API:
示例:
<p>Hello there</p><p>Javascripti</p>
我想在我的React Native应用程序中使用它,但这次我要替换<p>
的所有<Text>
标签,因为这是我可以在React Native中使用的。
我怎样才能实现这样的目标?我无法找到如何翻译或解析HTML。
编辑:我使用正则表达式使<p>
成为<Text>
,但它只返回一个字符串。它在屏幕上显示:<Text>Lorem Ipsum</Text
。我如何解析这个可读的JSX?
答案 0 :(得分:1)
JSX基本上是用于调用createElement
的语法糖(见here,here和especially here):
React.createElement(component, props, ...children)
因此,一旦您捕获了<p></p>
标记内的文字,就可以生成如下标记:
let capturedContent = 'Hello there'
const element = React.createElement('Text', { }, capturedContent)
答案 1 :(得分:0)
可以在这里使用正则表达式:
标签已更改
const myRegex = /(<p>|<\/p>)/g;
const myHtml = '<p>Hello there</p><p>Javascripti</p>';
myHtml.replace(myRegex, 'Text');
然后将其放入您的jsx的React-Native渲染中。这将是这样的。我不知道你的完整设置如此道歉:
render() {
return (
{myHtml}
)
}