Javascript allow only specific HTML tags - Stack Overflow

I have a long HTML string, and I would like to parse it to allow only certain html tags to pass through

I have a long HTML string, and I would like to parse it to allow only certain html tags to pass through.

The allowed tags are bold, italics, underline, paragraph, ordered list, and unordered list.

These tags and their corresponding text should be returned. All other tags should be removed, and only the innerHTML should be left for those.

For the following inputs, the outputs should be as such:

Input: <p>There is some <u>text</u> here</p> Output: <p>There is some <u>text</u> here</p>

Input: <span style="color: rgb(34, 34, 34); font-family: arial; font-size: small;">text here</span> Output: text here

Input: <div>Combo of <b>allowed</b> tag and <i>disallowed</i> tag</div> Output: Combo of <b>allowed</b> tag and <i>disallowed</i> tag

I have looked at this similar question: Regex to allow only set of HTML Tags and Attributes. But, I was wondering how you would do it in Javascript? I am currently iterating through the html and doing a search for the allowed tags like this:

var htmlString = "<p>There is some <u>text</u> here</p>";
var allowedTags = ["<b>", "<i>", "<u>", "<p>", "<ol>", "<ul>"];
for (i = 0, len = allowedTags.length; i < len; i++) {
    var ind = htmlString.indexOf(allowedTags[i]);
}

But this doesn't work, especially if you have multiple html tags in a string. Much appreciation for your help!

I have a long HTML string, and I would like to parse it to allow only certain html tags to pass through.

The allowed tags are bold, italics, underline, paragraph, ordered list, and unordered list.

These tags and their corresponding text should be returned. All other tags should be removed, and only the innerHTML should be left for those.

For the following inputs, the outputs should be as such:

Input: <p>There is some <u>text</u> here</p> Output: <p>There is some <u>text</u> here</p>

Input: <span style="color: rgb(34, 34, 34); font-family: arial; font-size: small;">text here</span> Output: text here

Input: <div>Combo of <b>allowed</b> tag and <i>disallowed</i> tag</div> Output: Combo of <b>allowed</b> tag and <i>disallowed</i> tag

I have looked at this similar question: Regex to allow only set of HTML Tags and Attributes. But, I was wondering how you would do it in Javascript? I am currently iterating through the html and doing a search for the allowed tags like this:

var htmlString = "<p>There is some <u>text</u> here</p>";
var allowedTags = ["<b>", "<i>", "<u>", "<p>", "<ol>", "<ul>"];
for (i = 0, len = allowedTags.length; i < len; i++) {
    var ind = htmlString.indexOf(allowedTags[i]);
}

But this doesn't work, especially if you have multiple html tags in a string. Much appreciation for your help!

Share edited May 23, 2017 at 12:33 CommunityBot 11 silver badge asked Jul 7, 2015 at 3:34 llams48llams48 4072 gold badges8 silver badges17 bronze badges 1
  • Use a library to do this. A quick google will turn up some good ones. – user663031 Commented Jul 7, 2015 at 3:44
Add a ment  | 

1 Answer 1

Reset to default 8

It looks like you want to emulate php in javascript.
You could try phpjs function strip_tags see here

function strip_tags(input, allowed) {
//  discuss at: http://phpjs/functions/strip_tags/
// original by: Kevin van Zonneveld (http://kevin.vanzonneveld)
// improved by: Luke Godfrey
// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld)
//    input by: Pul
//    input by: Alex
//    input by: Marc Palau
//    input by: Brett Zamir (http://brett-zamir.me)
//    input by: Bobby Drake
//    input by: Evertjan Garretsen
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld)
// bugfixed by: Onno Marsman
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld)
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld)
// bugfixed by: Eric Nagel
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld)
// bugfixed by: Tomasz Wesolowski
//  revised by: Rafał Kukawski (http://blog.kukawski.pl/)
//   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://kevin.vanzonneveld'>Kevin van Zonneveld</a>", "<a>");
//   returns 3: "<a href='http://kevin.vanzonneveld'>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'

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

just use it like this :

var str = strip_tags(
   '<p>There is some <u>text</u> here</p>',
   '<b><i><u><p><ol><ul>' // Allowed tags
);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742290859a4416158.html

相关推荐

  • Javascript allow only specific HTML tags - Stack Overflow

    I have a long HTML string, and I would like to parse it to allow only certain html tags to pass through

    21小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信