如何仅显示字符串中的标签 <h1>,reactjs

Bay*_*ayu 5 html javascript tags string reactjs

如何仅显示变量字符串中的标签,

const textHtml = "<h1>What events are you looking for today?</h1> <p>Find more events you want!</p>"
Run Code Online (Sandbox Code Playgroud)

输出:您今天正在寻找什么活动?寻找更多您想要的活动!

预期:您今天正在寻找什么活动?只显示标签 h1、标签 p,我不会显示标签 p

并且输出仅显示标记,<h1>并且标记<p>被删除/不显示,原因值字符串textHtml是来自 api 响应的值。我不知道如何只显示标签<h1>

有人可以帮我吗?抱歉我的英语不好,谢谢

Roh*_*dal 5

<p>因为您想从字符串中删除整个元素textHtmlRegEx您可以通过使用方法的帮助轻松实现它String.replace()

现场演示

const textHtml = "<h1>What events are you looking for today?</h1> <p>Find more events you want!</p>"

const res = textHtml.replace(/<p>*.*<\/p>/, '');

console.log(res);
Run Code Online (Sandbox Code Playgroud)