Skip to content

Commit bd14bbb

Browse files
fix regex to not mix self closing tags
1 parent 4a6cabe commit bd14bbb

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

Docs/Types/String.Extras.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Remove all html tags from a string.
137137
var noItalics = html.stripTags('i');
138138
//returns "<b>This is a string with html in it.</b>"
139139
var noItalicsContent = html.stripTags('i', true);
140-
returns "<b>This is a string with in it.</b>"
140+
//returns "<b>This is a string with in it.</b>"
141141

142142
### Returns
143143

Source/Types/String.Extras.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ var walk = function(string, replacements){
9595
};
9696

9797
var getRegexForTag = function(tag, contents){
98-
tag = tag || '';
99-
var regstr = contents ? "<" + tag + "(?!\\w)[^>]*>([\\s\\S]*?)<\/" + tag + "(?!\\w)>" : "<\/?" + tag + "([^>]+)?>";
98+
tag = tag || (contents ? '' : '\\w+');
99+
var regstr = contents ? "<" + tag + "(?!\\w)[^>]*>([\\s\\S]*?)<\/" + tag + "(?!\\w)>" : "<\/?" + tag + "\/?>|<" + tag + "[\\s|\/][^>]*>";
100100
return new RegExp(regstr, "gi");
101101
};
102102

Specs/Types/String.Extras.js

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ describe('String.stripTags', function(){
5050
expect('i like cookies'.stripTags()).toEqual('i like cookies');
5151
});
5252

53+
it('should leave a string with <b></b> but no <br/>', function(){
54+
expect('<b>Header</b><br/>'.stripTags('br')).toEqual('<b>Header</b>');
55+
});
56+
57+
it('should leave a string with <br/> but no <b></b>', function(){
58+
expect('<b class="foo">Header</b><br/>'.stripTags('b')).toEqual('Header<br/>');
59+
});
60+
5361
});
5462

5563
describe('String.truncate', function(){

0 commit comments

Comments
 (0)