javascript 如何从反应原生的json数据中删除html标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48766615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:16:01  来源:igfitidea点击:

How to remove html tags from json data in react native?

javascripthtmljsonreact-native

提问by Linu Sherin

In my React Native app i'm fetching json data which contains raw html tags.How to remove the html tags from that?Following is the json response i'm getting

在我的 React Native 应用程序中,我正在获取包含原始 html 标签的 json 数据。如何从中删除 html 标签?以下是我得到的 json 响应

[{
    "data": {
        "course": {
            "id": 2864,
            "name": "2. Understanding India's economic transition",
            "date_created": 1506154480,
            "status": "publish",
            "price": false,
            "price_html": "FREE",
            "total_students": 0,
            "seats": "",
            "start_date": false,
            "average_rating": 0,
            "rating_count": 0,
            "featured_image": "https://www.mywebsite.com/lms/wp-content/themes/wplms/assets/images/avatar.jpg",
            "categories": [],
            "instructor": {
                "id": "22",
                "name": "aami",
                "avatar": "https://www.mywebsite.com/lms/wp-content/uploads/2017/11/favicon.png",
                "sub": ""
            },
            "menu_order": 0
        },
        "description": "<p style=\"text-align: justify;\">India is undergoing an economic, social and technological transformation. Perhaps no other phase is going to be as critical as the present one in shaping the future of the country as well as determining the welfare of the people. The economic transition is the vital ingredient of these overall change. Faster growth accompanied by industrial sector expansion, skill addition to the people, creation of quality infrastructure etc. will fuel this growth phase.</p>\n<p style=\"text-align: justify;\">For understanding how important is the present development phase, we have to adopt a historical and comparative study. Following factors helps us to understand the present development phase of India.</p>\n\n<ol style=\"text-align: justify;\">\n \t<li><strong>Achievement of higher growth rate as a middle-income economy. </strong></li>\n</ol>\n<p style=\"text-align: justify;\">India at present is a lower middle-income economy and has to become a high-income economy undergoing a rapid growth phase extending at least three decades.</p>\n<p style=\"text-align: justify;\">Most important narrative about the Indian economy is that it is the third largest in the world in terms of Purchasing Power Parity GDP. But a superior way to asses a country's development is to consider per capita income. The widely used ranking about countries' economic position is that of the World Bank's GDP Per capita (constant US $) and the data for 2016 shows that India's per capita income is 61 compared to China's $ 6994.</p>\n<p style=\"text-align: justify;\">Table: Categorization of countries by World Bank</p>\n\n<table>\n<tbody>\n<tr>\n<td width=\"198\"><strong>Category</strong></td>\n<td width=\"318\"><strong>PCI as on 2015 in constant US Dollar</strong></td>\n</tr>\n<tr>\n<td width=\"198\">Low Income Economy</td>\n<td width=\"318\">$ 1025 or less</td>\n</tr>\n<tr>\n<td width=\"198\">Middle Income Economy\n\n(India - $ 1861)</td>\n<td width=\"318\">26 to 35 (Lower Middle Income)\n\n36 to 475 (Upper Middle Income)</td>\n</tr>\n<tr>\n<td width=\"198\">Higher Income Economy</td>\n<td width=\"318\">$ 12476 and above</td>\n</tr>\n</tbody>\n</table>\n<p style=\"text-align: justify;\">According to World Bank metrics, a country with less than 45 is considered as low-income economy whereas one with a PCI of 736 or higher is considered as a higher income economy. Higher income means higher standard of living. India is at the bottom of the lower middle-income economies and has to achieve higher economic growth, structural changes including industrialisation to raise per capita income near to the 736 mark in the long run. This is what the country has to achieve through the transition.</p>\n\n<ol style=\"text-align: justify;\" start=\"2\">\n \t<li><strong> Industrial sector expansion</strong></li>\n</ol>\n<p style=\"text-align: justify;\">There are several factors that drives the economy to prosperity. Per capita income is just a monitored goal. How it can be raised is through achieving more productivity and employment generation in the sectors that can create big changes is decisive element for the country's transition. Here comes the role of industrial sector. The industrial sector is known for generating huge employment with minimum skill addition. Similarly, no other sector has higher level of tradability as the industrial sector (means a country can earn big income through exports). Graduating to an expanded services sector without undergoing industrialisation will be self-defeating and unsuitable to a big economy like India. Depending on services sector for exports will not reward as other countries like to protect their services sector from the inflow of India's skilled persons. If India can increase the contribution of the industrial sector in GDP from the present 30 per cent to say 40-45%, it implies that sizable income and employment are created in the sector.</p>\n\n<ol style=\"text-align: justify;\" start=\"3\">\n \t<li><strong> Skilling the people when demography favours.</strong></li>\n</ol>\n<p style=\"text-align: justify;\">India has the largest number of young people in the world besides having largest workforce age group population. This situation is expected to remain till 2045. Now, youth means higher ability to produce, consume and thus stimulate overall economic activities. As in the case of an individual, better things happen for an economy when it is young.</p>\n\n<ol style=\"text-align: justify;\" start=\"4\">\n \t<li><strong> Infrastructure generation.</strong></li>\n</ol>\n<p style=\"text-align: justify;\">Infrastructure is the platform for fueling growth. The government has launched several programmes to build quality infrastructure to assist economic transformation. In the industrial sector, there is the industrial corridor project; in transportation – there is the NHDP, PMGSY, Bhartamala, Sagarmala etc. Similarly, digital infrastructure is undergoing a qualitative improvement along with the education sector.</p>\n\n<ol style=\"text-align: justify;\" start=\"5\">\n \t<li><strong> Building invention and Innovation.</strong></li>\n</ol>\n<p style=\"text-align: justify;\">Development is dynamic and present day developing economies can't achieve development with the strategy of the past. Fourth industrialisation, robotics, artificial intelligence etc., proved that industrialisation is not labour oriented. Here, Countries like China with the aid of superior technology with its sizable labour force can produce and supply goods to the entire world. Competing in the new age industrial sector need good technological adaptation and a progressive national invention and educational systems.</p>\n\n<ol style=\"text-align: justify;\" start=\"6\">\n \t<li><strong> Easing of Doing Business.</strong></li>\n</ol>\n<p style=\"text-align: justify;\">India's business environment is historically suffocated by excess regulations and slow bureaucratic functions that are unsuitable for enterprise development.. But in recent years, institutional reforms are taking place and development blocking regulations are in the process of elimination.</p>\n<p style=\"text-align: justify;\">Development has become a major theme of the government and every issue related with economic prosperity are sophisticatedly addressed. Improvements in all the above field is slowly yielding results. Despite the adverse global slowdown and anti-globalisation headwinds, India continues to be the fastest growing large economy.</p>",
        "curriculum": false,
        "reviews": [],
        "instructors": [{
            "id": "22",
            "name": "tojo",
            "avatar": "https://www.mywebsite.com/lms/wp-content/uploads/2017/11/favicon.png",
            "sub": "",
            "average_rating": 0,
            "student_count": 0,
            "course_count": "0",
            "bio": false
        }],
        "purchase_link": false
    },
    "headers": [],
    "status": 200
}]

I'm listing descriptioninside a card. So how to remove html tags?Please do help..Is there a way to do this?

我在description卡片里面列出。那么如何删除html标签?请帮忙..有没有办法做到这一点?

回答by Sid

What about this?

那这个呢?

const regex = /(<([^>]+)>)/ig;
const result = data.description.replace(regex, '');

回答by Louis Lecocq

I also faced this challenge in my app. After trying several library, i'd like to recommand you react-native-render-html

我在我的应用程序中也遇到了这个挑战。在尝试了几个库之后,我想推荐你react-native-render-html

It allows you to provide 'html' to a Native component and have a simple text as an output. You can also customize each tag to give them specific style or ignore some of them.

它允许您向本机组件提供“html”并具有简单的文本作为输出。您还可以自定义每个标签以赋予它们特定的样式或忽略其中的一些。

It does not require you to use 'react link' to make it work so you can use it in your expo app as well.

它不需要您使用“反应链接”来使其工作,因此您也可以在您的博览会应用程序中使用它。

Hope it helps.

希望能帮助到你。