Reputation: 68396
Is there a jQuery version of this function?
string strip_tags( string $str [, string $allowable_tags ] )
strip all tags and content inside them from a string except the ones defined in the allowable tags string.
like:
var stripped = strip_tags($('#text').html(), '<p><em><i><b><strong><code>');
from:
<div id="text">
<p> paragraph </p>
<div> should be stripped </div>
</div>
Upvotes: 37
Views: 56289
Reputation: 119
Even if this is an old thread i think it could be useful for those who are still looking for an answer.
The Locutus.io function seems to be the best solution:
function strip_tags (input, allowed) {
allowed = (((allowed || '') + '').toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('')
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi
var commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi
return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : ''
})
}
Example 1:
strip_tags('<p>Kevin</p> <br /><b>van</b> <i>Zonneveld</i>', '<i><b>')
returns 1: 'Kevin <b>van</b> <i>Zonneveld</i>'
Example 2:
strip_tags('<p>Kevin <img src="someimage.png" onmouseover="someFunction()">van <i>Zonneveld</i></p>', '<p>')
returns 2: '<p>Kevin van Zonneveld</p>'
Example 3:
strip_tags("<a href='http://kvz.io'>Kevin van Zonneveld</a>", "<a>")
returns 3: "<a href='http://kvz.io'>Kevin van Zonneveld</a>"
Example 4:
strip_tags('1 < 5 5 > 1')
returns 4: '1 < 5 5 > 1'
Example 5:
strip_tags('1 <br/> 1')
returns 5: '1 1'
Example 6:
strip_tags('1 <br/> 1', '<br>')
returns 6: '1 <br/> 1'
Example 7:
strip_tags('1 <br/> 1', '<br><br/>')
returns 7: '1 <br/> 1'
Upvotes: 8
Reputation: 710
To remove all tags, could use:
var StrippedString = OriginalString.replace(/(<([^>]+)>)/ig,"");
Code from: Strip HTML Tags in JavaScript
Upvotes: 4
Reputation: 5139
This worked for me:
function strip_tags(str) {
str = str.toString();
return str.replace(/<\/?[^>]+>/gi, '');
}
Upvotes: 11
Reputation: 4259
Just use a regular expression:
html.replace( /<.*?>/g, '' );
Done. :)
For the p
tag:
html.replace( /<[^p].*?>/g, '' );
For other tags, it gets more complicated.
Upvotes: 14
Reputation: 2514
You can try this, probably best solution: http://phpjs.org/functions/strip_tags/
Upvotes: 2
Reputation: 342625
To remove just the tags, and not the content, which is how PHP's strip_tags()
behaves, you can do:
var whitelist = "p"; // for more tags use the multiple selector, e.g. "p, img"
$("#text *").not(whitelist).each(function() {
var content = $(this).contents();
$(this).replaceWith(content);
});
Upvotes: 36
Reputation: 42808
Use the following to strip tags while keeping content
$('#text').find('p').contents().unwrap();
This will strip p
tag where p
is a child element of '#text'.
Upvotes: 1
Reputation: 21368
Not an actual answer, but a word of caution (depending on what you're trying to do with this):
IMHO, in almost all cases, input sanitization should be done on the server side (in this case, using the native PHP functions). If your intent is to replace PHP functionality with client-side functionality, I would strongly advise against it.
Why?
Just because you're authoring a website, it doesn't mean that:
Again, not really answering your question, but a word of caution based on where you could possibly be headed based on your question :)
Upvotes: 4