正则表达式或用于从字符串中提取<a></a>的函数

时间:2019-03-13 21:51:13

标签: javascript reactjs

我正在使用React&Typsescript。我有一段来自API的文本,我需要从中提取标记,以便可以在其上放置onClick来报告信息(知道有人单击它)。我当时正在考虑创建一个变量,可以将其设置为标签,然后对其进行onclick。这是代码块:

"<div id="wfText">
<p>In order to view your available regional sports network and local channels, you have to be located within your billing zip code.</p><ul><li>Sometimes even though you are located within your billing zip code, your internet provider routes the internet service through a different zip code. The DIRECTV NOW app uses the zip code provided by them to determine location. 
</li>
</ul>
<div>
<a id="link" data-toggle="collapse" data-target="#par1" class="kbarticle-expandable collapsed"> <span style="vertical-align: -webkit-baseline-middle;">View Available Channels</span> </a><div id="par1" class="collapse" style="background: none; height: 0px;"><p>If you are registering in a different zip code than your billing zip code, to see what channels are available for you to view:</p><ol><li>Go to 

<a target="_blank" href="https://www.directv.com/rsnlocalchannels">directv.com/rsnlocalchannels</a>

</li><li>Enter your billing zip code and review the channels available. </li><li>Enter the zip code for the area you are registered in and look for networks that match the ones available with your billing zip code. </li></ol><p>If you don&#39;t see the same network on both lists then there will not be a local channel available for that network.</p></div></div><p>Issue resolved?</p>"

我需要获得一个动态链接,并在其上放置一个onClick链接到另一个功能。有谁知道正则表达式或操作此字符串以提取和所有内容的方式?需要帮助

1 个答案:

答案 0 :(得分:0)

function createElement(html, elem = "div") {
  const element = document.createElement(elem);
  element.innerHTML = html
  return element
}

const html = `<div id="wfText">
<p>In order to view your available regional sports network and local channels, you have to be located within your billing zip code.</p><ul><li>Sometimes even though you are located within your billing zip code, your internet provider routes the internet service through a different zip code. The DIRECTV NOW app uses the zip code provided by them to determine location. 
</li>
</ul>
<div>
<a id="link" data-toggle="collapse" data-target="#par1" class="kbarticle-expandable collapsed"> <span style="vertical-align: -webkit-baseline-middle;">View Available Channels</span> </a><div id="par1" class="collapse" style="background: none; height: 0px;"><p>If you are registering in a different zip code than your billing zip code, to see what channels are available for you to view:</p><ol><li>Go to 

<a target="_blank" href="https://www.directv.com/rsnlocalchannels">directv.com/rsnlocalchannels</a>

</li><li>Enter your billing zip code and review the channels available. </li><li>Enter the zip code for the area you are registered in and look for networks that match the ones available with your billing zip code. </li></ol><p>If you don&#39;t see the same network on both lists then there will not be a local channel available for that network.</p></div></div><p>Issue resolved?</p>`

console.log(createElement(html).querySelector("a"))