简体   繁体   中英

How can I remove this p tag from my json fetch

I'm using React and fetch to get this data from an api I removed the p tag that I had but is still appearing there and that not what I want. I think the text in the json data has ap tag. How I can removed it? 错误要处理

I'm using fetch on the lifecycle method componenDidMount:

componentDidMount() {
    fetch(API).then(response => response.json()).then(data => this.setState({ quotes: data}));
  }

How can I find & replace the p tag before I added to the state? I'm trying to do it right there in the lifecycle method but it keeps throwing me errors

This should do it:

 let demoJSON = { field: "<p>Hello World</p>" } console.log(demoJSON.field.replace(/(<([^>]+)>)/ig, "")); 

Alternatively to matbous answer using regex, you could just replace the tags directly like this:

let demoJSON = {
 field: "<p>Hello World</p>"
}

demoJSON.field.replace('<p>','').replace('</p>', '');

console.log(demoJSON.field);

componentDidMount() {
   fetch(API).then(response => response.json()).then(data => this.setState({ 
quotes: data.replace('<p>','').replace('</p>', '')}));
}

Should probably work, so should @Matou's answer in his comment. If not, can you share the actual error output so we can help further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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