Trupti
Trupti

Reputation: 983

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?

Upvotes: 0

Views: 231

Answers (2)

Mihai T
Mihai T

Reputation: 17697

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)

Upvotes: 1

Sonjoy Samadder
Sonjoy Samadder

Reputation: 77

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

Upvotes: 0

Related Questions