Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stripTags strips things that aren't tags #469

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
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