最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove all tags in html string except <a> - Stack Overflow

programmeradmin1浏览0评论

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,' ');
}

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,' ');
}
Share Improve this question edited Mar 4, 2014 at 11:59 ArVan asked Mar 4, 2014 at 10:18 ArVanArVan 4,2759 gold badges38 silver badges58 bronze badges 6
  • 1 ... stackoverflow./questions/1732348/… ... the general rule is that you cannot process arbitrary markup with regex. (If you have a special case that you only have a single a tag without plicated nesting, it might work. – Christoph Commented Mar 4, 2014 at 10:20
  • @musefan did I talk about parsing? – Christoph Commented Mar 4, 2014 at 10:23
  • @musefan The OP wants to remove all tags other than the a with help of a regex, or am I mistaken? In my eyes, that's processing html markup with a regex. – Christoph Commented Mar 4, 2014 at 10:30
  • I've found some interressting post in another thread. stackoverflow./a/5601929/3303652 This might help you. example: jsfiddle/JEnvr/229 – Marcel Commented Mar 4, 2014 at 10:54
  • I'm not forced to use RegExp, so any solution is fine :) – ArVan Commented Mar 4, 2014 at 11:57
 |  Show 1 more ment

2 Answers 2

Reset to default 6

Try this:

$(string).find(':not(a)').contents().unwrap()

This will vorky with every piece of html code.

Example: http://jsfiddle/E3RWL/1/

Here's a function I found for you:

You can read more about this at: http://phpjs/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,
    mentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
  return input.replace(mentsAndPhpTags, '').replace(tags, function ($0, $1) {
    return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
  });
}
发布评论

评论列表(0)

  1. 暂无评论