I would like to remove <p align="left" dir="ltr"> </p> <p> </p>
tags from string.
str.replace(/\s| /g, '')
I need to format String as it will be part of Email template, It is not complete HTML
I would like to remove <p align="left" dir="ltr"> </p> <p> </p>
tags from string.
str.replace(/\s| /g, '')
I need to format String as it will be part of Email template, It is not complete HTML
Regex is the wrong tool for this.
If you're doing it in a browser, it's easy:
var div = document.createElement('div');
div.innerHTML = str;
Array.prototype.slice.call(div.querySelectorAll('p'), function(p) {
var html = p.innerHTML.trim();
if (!html || html.toLowerCase() == " ") {
p.parentNode.removeChild(p);
}
});
str = div.innerHTML; // Yes, the case of tag names may have changed, etc., but nothing substantive
If you're doing it in another environment, there's an HTML parser available for that environment. NodeJS has several, including cheerio. The JVM (if you're using JavaScript on the JVM) has the excellent JSoup. .Net (if you're using "JScript") has a port of JSoup. Etc.