简体   繁体   English

ReactJs:如何逃脱 <p> 包含道具的数据渲染

[英]ReactJs: How to escape <p> enclosing data rendering from props

How could I escape <p> enclosing data that are coming from Back-end in props . 我该如何逃避<p>包含来自props来自后端的数据。 I want to display my texts without this <p> enclosing tags around the texts. 我想显示我的文本而在文本周围没有这个<p>括起来的标记。

ex: 例如:

props {
 explain: "`<p>`!! Check-in `</p>`"
}

how could i display this as 我怎么能显示为

`props.explain` as "!! Check-in"

Any suggestions 有什么建议么

If you prop would have name explain you can use regex: explain.replace(/<p[^>]*>|<\\/p[^>]*>/g, ""); 如果您的道具有名称explain ,则可以使用正则表达式: explain.replace(/<p[^>]*>|<\\/p[^>]*>/g, "");

so in react code you could use: 所以在反应代码中,您可以使用:

render() {
  const { explain } = this.props
  return (
    { explain.replace(/<p[^>]*>|<\/p[^>]*>/g, "") }
  )
}```

The proper way would be to remove the html tags from backend. 正确的方法是从后端删除html标签。 However if you can't do that: 但是,如果您不能这样做:

myVariable.replace(/<[^>]*>/g, "")

It would remove all the html tags from a given string. 它将删除给定字符串中的所有html标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM