简体   繁体   中英

Remove html tags from json response in reactsjs

I am confused about how I can remove HTML tags from a JSON response in reactjs. This is the JSON response:

{
    "price": "26,800.98",
    "diff": "<!--daily_changing-->+13.44 (+0.05%)&nbsp;&nbsp;<span class=\"icon-arrow-dark-circle-right-up zoom-icon\" aria-hidden=\"true\"></span>",
    "diff_xs": "<!--daily_changing-->+13.44 (+0.05%)&nbsp;<span class=\"icon-arrow-dark-circle-right-up zoom-icon\" aria-hidden=\"true\"></span>",
    "price_diff": "<!--daily_changing--><div class=\"current_sub_pos\"><span class=\"current_sub_price\">26,800.98&nbsp;</span>+13.44 (+0.05%)&nbsp;&nbsp;<span class=\"icon-arrow-dark-circle-right-up zoom-icon\" aria-hidden=\"true\"></span>"
}

I want to get diff value +13.44 within react

Using your_string.replace(/<[^>]*>?/gm, ''); will remove the html

You can try something like this.

const jsonData = {
"price": "26,800.98",
"diff": "<!--daily_changing-->+13.44 (+0.05%)&nbsp;&nbsp;<span class=\"icon-arrow-dark-circle-right-up zoom-icon\" aria-hidden=\"true\"></span>",
"diff_xs": "<!--daily_changing-->+13.44 (+0.05%)&nbsp;<span class=\"icon-arrow-dark-circle-right-up zoom-icon\" aria-hidden=\"true\"></span>",
"price_diff": "<!--daily_changing--><div class=\"current_sub_pos\"><span class=\"current_sub_price\">26,800.98&nbsp;</span>+13.44 (+0.05%)&nbsp;&nbsp;<span class=\"icon-arrow-dark-circle-right-up zoom-icon\" aria-hidden=\"true\"></span>"
}
const deleteHtml = jsonData.replace(/<\/?[^>]+>/gi, '');
console.log(deleteHtml);

see if this works.

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