Passe Agora
Passe Agora

Reputation: 25

Whitelist javascript to strip html tags

I have modified a whitelist javascript regex that strip unwanted tags.

I am trying to allow this code:

<span style="color: #000000"></span>

but I am unable to do it in regex.

Bellow is what is have so far:

(/<(?!(br|\/br|p|\/p|b|\/b|u|\/u|ol|\/ol|ul|\/ul|li|\/li))([^>])+>/gi

Thanks

Upvotes: 1

Views: 2374

Answers (3)

Hardik Sondagar
Hardik Sondagar

Reputation: 4495

I have developed tool with Source code. It'll strip all the tags with exception list proveded by user : try this HTML Tag Stripper

Upvotes: 1

Joanna Derks
Joanna Derks

Reputation: 4063

Works for me as well - unless there is more that you are trying to do - e.g. if there is any content between the tags, or if you want to match the opening and closing tag in the same run - then post the example in your question.

BTW: the regex can be simplified a little the following way:

<(?!((?:\/\s*)?(?:br|p|b|u|[o|i]l|li)))([^>])+>
  • (?:\/\s*)? - an optional slash
  • (?:br|p|b|u|[o|i]l|li) - followed by any of these tags

UPDATE:

Here's my last try:

  • if you want to match all the other tags use this

    <(?!(?:\/\s*)?(?:br|p|b|[o|u]l|li|span)(?:\s*style='color: #[A-Fa-f0-9]+'))([^>])*>

  • if you want to match the tags with color use this

    <((?:\/\s*)?(?:br|p|b|[o|u]l|li|span)(?:\s*style='color: #[A-Fa-f0-9]+'))([^>])*>

Upvotes: 3

Michael Frederick
Michael Frederick

Reputation: 16714

this works for me (no parenthesis at the beginning):

/<(?!(br|\/br|p|\/p|b|\/b|u|\/u|ol|\/ol|ul|\/ul|li|\/li))([^>])+>/gi

Upvotes: 2

Related Questions