Hi I am trying to remove all the html tags from a particular string its showing error.
Here is my string:
<p>Hi there</p> ~ wifi free <p>this is test</p> ~ breakfast free <p>This is another test</p>
My jQuery code is here:
var item = <p>Hi there</p> ~ wifi free <p>this is test</p> ~ breakfast free <p>This is another test</p>;
item = item.replace(/~/g, '');
item = item.replace(/<p>/g, '');
item = item.replace('</p>'/g, '');
var splitArray = item.split('<br />');
var l = splitArray.length;
for (var i = 0; i < l; i++) {
out = out + "<li><span class='sp_icon sp_star_icon'></span> "
+ splitArray[i].trim() + "</li>";
}
console.log(item);
With vanilla JS you can do it like this
var item = '<p>Hi there</p> ~ wifi free <p>this is test</p> ~ breakfast free <p>This is another test</p>';
function getText(html) {
var tmp = document.createElement('div');
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText;
}
console.log(getText(item));
You can strip out all the html-tags with a regular expression: /<(.|\n)*?>/g
Described in detail here: http://www.pagecolumn.com/tool/all_about_html_tags.htm
In your JS-Code it would look like this:
item = item.replace(/<(.|\n)*?>/g, '');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With