Reputation: 4275
I'v got some html which I must clear from all tags except one concrete <a>
with known class.
Here is the html:
var string = '<span class="so_sentence"><span> Some text <a class="so_footnote-ref" href="#footnote-104008-4" id="footnote-104008-4-backlink">[1]</a></span></span>';
I have JQuery attached, so I get the jQuery object of the string.
var html = $(string);
Now I have to clear the string from all the span and probably other tags, except this <a>
:
<a class="so_footnote-ref" href="#footnote-104008-4" id="footnote-104008-4-backlink">[1]</a>
So my final string should be:
'Some text <a class="so_footnote-ref" href="#footnote-104008-4" id="footnote-104008-4-backlink">[1]</a>'
Also it must be possible to call this function on the result, so it must be of appropriate type:
function _trim(string){
return string.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ');
}
Upvotes: 2
Views: 3369
Reputation: 375
Here's a function I found for you:
You can read more about this at: http://phpjs.org/functions/strip_tags/
Javascript:
var ret = strip_tags('<p>Kevin</p> <br /><b>van</b> <i>Zonneveld</i>', '<i><b>');
// returns: 'Kevin <b>van</b> <i>Zonneveld</i>'
function strip_tags (input, allowed) {
allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
});
}
Upvotes: 2
Reputation: 1010
Try this:
$(string).find(':not(a)').contents().unwrap()
This will vorky with every piece of html code.
Example: http://jsfiddle.net/E3RWL/1/
Upvotes: 6