简体   繁体   English

如何从状态数据的字符串中删除HTML标记?

[英]How can I remove HTML markup from strings in state data?

  1. I fetch data from an API using 'axios'. 我使用“ axios”从API提取数据。
  2. I save the data recieved in my State. 我保存了本州收到的数据。
  3. I output the information given from the state in a table. 我将状态给出的信息输出到表中。

The issue is that the Strings I recieve from the fetched data include a lot of HTML tags such as < p > , < h1 > & < br > I would like to remove these if possible. 的问题是,我从取出的数据收到的字符串包含大量HTML标记的如< p > < h1 > < br >我想如果可能的话清除这些。

Note that I do not use JQuery 请注意,我不使用JQuery

state = {
  companyName: "Loading",
      loading: true,
         data: [] //THIS IS WHERE I STORE ALL THE DATA THAT I FETCH
}
data(props) {
  return(
    <div>
      <Table celled>
        <Table.Header>
          <Table.Row>
            <Table.HeaderCell>Index</Table.HeaderCell>
            <Table.HeaderCell>Författning</Table.HeaderCell>

          </Table.Row>
        </Table.Header>

        {props.map((lawList, index) => (
                    <Table.Body>
                      <Table.Row>
                        <Table.Cell>{index + 1}</Table.Cell>
/* The line below displays the data, but the data includes alot of html tags that i wish to have removed */
                        <Table.Cell>{lawList.lawDTO.name}</Table.Cell>
                      </Table.Row>
                    </Table.Body>
            ))}
      </Table>
    </div>
  )
}
render() {
    return(
        <div>
                <h3> {this.data(this.state.data)} </h3>

        </div>   
     )}
   }

You can use a regular expression: 您可以使用正则表达式:

 const test = '<br>asdasd</br><p>test</p>'.replace(/<.{1,2}>|<.{2,3}>/g, ''); console.log(test); 

You could do something like this, as soon aj you have read your data: 您可以执行以下操作,一旦您读取了数据:

 let data = "<p>first <h1> second <br> third </p>"; data = data.replace(/<\\/?[^>]+(>|$)/g, ''); console.log(data); 

Possibly, you want to render the html ? 您可能要呈现html吗?

render() {
  return (
    <div dangerouslySetInnerHTML={{ __html: this.state.data }}></div>
  )
}

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

相关问题 如何删除页面上不需要的HTML标记? - How can I remove unwanted piece of HTML markup on the page? 如何从HTML TextNode而不是HTML标记获取实际显示的文本? - How can I get the actual displayed text from an HTML TextNode instead of the HTML markup? 如何用javascript或jquery替换或删除标记中的包装div并将动态内容保留在原位? - How can I replace or remove a wrapping div from markup with javascript or jquery and keep the dynamic contents in its place? 如何在不显示 html 标记的情况下过滤和标记输入中的单词? - How can I filter and mark words from an input without html markup being displayed? 如何从Javascript中的数组中删除带有空字符串的对象? - How can I remove objects with empty strings from array in Javascript? 如何将子元素添加到 HTML 标记中的自定义元素? - How can I add child elements to a custom element in HTML markup? 如何使用Regex从句子中删除特定的字符串? - How can I remove specific strings from a sentence using Regex? 如何从javascript中的字符串数组中删除动态文本 - How can I remove dynamic text from an array of strings in javascript 如何使用javascript验证HTML标记,然后显示其预览? - How can I validate HTML markup with javascript, and then show a preview of it? 如何基于 object 层次结构打印出 HTML 标记? - How can I print out HTML markup based on an object hierarchy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM