简体   繁体   中英

How to remove tag from the data in javascript?

I have one function having data as parameter and that data contains following as value:

<a class="result-tooltip" title="" href="/app.php/na_test_na/en/score/view/charted/1/1" data-original-title="<div style=&quot;margin: 0 auto; text-align: left; font-size: medium;&quot;>
<h4>Met: <b>15189</b>
<br>Total: <b>15189</b>
</h4>
</div>">
100.00%
</a>

I tried

data = data.replace(/<a.*">/, '').replace('</a>', '').trim();

But it is not working.

I want only 100.00% from the above. How can I get it?

If the data value is just a string ( i am not really sure how you pass that to your data variable ) you can make some ' tweaks ' to it.

Create a HTML element with javascript using document.createElement , add data as it's innerHTML then get the innerText which would be 100.00% and assign that value to data .

I changed a bit the value of data because what you pasted in your question was not valid HTML.

I am sure there must be other ways to achieve this, but this is what i've came out with:)

 let data = `<a class="result-tooltip" title="" href="/app.php/na_test_na/en/score/view/charted/1/1" data-original-title="<div style='margin: 0 auto; text-align: left; font-size: medium'> <h4>Met: <b>15189</b> <br>Total: <b>15189</b> </h4> </div>"> 100.00% </a>` const elem = document.createElement('div') elem.innerHTML = data data = elem.innerText.trim() console.log(data)

Try data.replace(/<\/?[^>]+(>|$)/g, '')

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