Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion stripTags.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var makeString = require('./helper/makeString');
var tagsAndComments = /<\/?([a-z][a-z0-9]*)\b[^>]*>|<!--[\s\S]*?-->/gi;

module.exports = function stripTags(str) {
return makeString(str).replace(/<\/?[^>]+>/g, '');
return makeString(str).replace(tagsAndComments, '');
};
5 changes: 5 additions & 0 deletions tests/stripTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ test('#stripTags', function() {
equal(stripTags('a <a href="#">link</a>'), 'a link');
equal(stripTags('a <a href="#">link</a><script>alert("hello world!")</scr'+'ipt>'), 'a linkalert("hello world!")');
equal(stripTags('<html><body>hello world</body></html>'), 'hello world');
equal(stripTags('<h1 id="foo" data-foo="bar">hello world</body></h1>'), 'hello world');
equal(stripTags('<web-component>hello world</web-component>'), 'hello world');
equal(stripTags('<ReactComponent.Title>hello world</ReactComponent.Title>'), 'hello world');
equal(stripTags('I have < I want, but that is > nothing'), 'I have < I want, but that is > nothing');
equal(stripTags('<!-- a html comment --->hello world<!-- a html comment --->'), 'hello world');
equal(stripTags(123), '123');
equal(stripTags(''), '');
equal(stripTags(null), '');
Expand Down