admin管理员组文章数量:1425717
how to display only tag in variable string,
const textHtml = "<h1>What events are you looking for today?</h1> <p>Find more events you want!</p>"
output : What events are you looking for today? Find more events you want!
expected : What events are you looking for today? just show the tag h1, the tag p, i won't the tag p is showing
and the output only display tag <h1>
and the tag <p>
is delete/not showing, cause value string of textHtml
is a value from api response. And i don't how to display only the tag <h1>
.
can anyone help me, please? And sorry for my bad english, thankyou
how to display only tag in variable string,
const textHtml = "<h1>What events are you looking for today?</h1> <p>Find more events you want!</p>"
output : What events are you looking for today? Find more events you want!
expected : What events are you looking for today? just show the tag h1, the tag p, i won't the tag p is showing
and the output only display tag <h1>
and the tag <p>
is delete/not showing, cause value string of textHtml
is a value from api response. And i don't how to display only the tag <h1>
.
can anyone help me, please? And sorry for my bad english, thankyou
Share Improve this question edited Aug 31, 2022 at 4:31 BayBay asked Aug 30, 2022 at 12:48 BayBayBayBay 1075 bronze badges 3-
If I am not correct, you want to remove the content of
<p>
element fromtextHtml
? – Rohìt Jíndal Commented Aug 30, 2022 at 13:56 -
yes, i want to delete
<p>
, – BayBay Commented Aug 31, 2022 at 4:27 - I added an answer. Hope it will work as per your expectation. Thanks. – Rohìt Jíndal Commented Aug 31, 2022 at 6:08
5 Answers
Reset to default 5As you want to remove the whole <p>
element from the textHtml
string. You can easily achieve it by using RegEx
with the help of String.replace()
method.
Live Demo :
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);
<h1>What events are you looking for today?</h1>
You're storing multiple HTML elements in a string, but you'll likely want to wrap multiple elements in a <Fragment>
, i.e.:
import { Fragment } from 'react'
const titleAndP = (
<Fragment>
<h1>What events are you looking for today?</h1>
<p>Find more events you want!</p>
</Fragment>
)
You need to wrap the code. You can do it with empty tags (typical in ReactJS)
const html = (
<>
<h1>title</h1>
<p>paragraph</p>
</>
)
Try adding this under your code
<Markup content={textHtml} />
source: https://www.codegrepper./code-examples/html/display+string+with+html+tags+react+js
本文标签: javascriptHow to display only tag lth1gt in stringreactjsStack Overflow
版权声明:本文标题:javascript - How to display only tag <h1> in string, reactjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745372993a2655814.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论