55997cec217233703cbf68b689578771

In my tests this works but I'm worried that I may be missing something.

var reg = /<\/?\w+\s?\/?>/ig;
var test = "<em>Risk &amp;<br />Compilance<br />Projects</em>";
test = test.replace(x, "");

alert( test );

Refactorings

No refactoring yet !

8fd61ba486a09ad5274dd70f1a1737f6

Sylvain

June 7, 2010, June 07, 2010 14:58, permalink

1 rating. Login to rate!

Your regex will break if it finds an angle bracket anywhere. There are a few other possible failures.

<[^<>]*?>
F9a9ba6663645458aa8630157ed5e71e

Ants

June 7, 2010, June 07, 2010 15:35, permalink

No rating. Login to rate!

I recommend reading:

http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html

Consider how to deal with:

<a href="http://a.link.he.re" target="blank">Rick Roll</a>
8f3486a6fd9309188a9560d432bf046d

strager

June 12, 2010, June 12, 2010 20:43, permalink

No rating. Login to rate!

Regular expressions are bad for HTML, as @Ants mentioned. You can use the DOM to do what you want:

var element = document.createElement('div');
var test = '<em>Risk &amp;<br />Compilance<br />Projects</em>';
element.innerHTML = test;
var plainText = element.textContent || element.innerText;
alert(plainText);

Your refactoring





Format Copy from initial code

or Cancel