2

just need a quick help for solving this problem.

I want to strip all html tags out of a string except the tags from a whitelist(variable).

My code so far:

whitelist = 'p|br|ul|li|strike|em|strong|a', 
reqExp = new RegExp('<\/?[^>|' + whitelist + ']+\/?>');

The problem is now it works more or less fine but also not removing for example b because it matches the b from the br out of the whitelist.

I tried different approaches but dont find the right solution. How can i tell the regex to do something like /.WITHOUT(smth)/ (therefore: match all expect everything following).

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
user1104419
  • 77
  • 1
  • 6
  • You'd be better off using a callback for the replacement and examining the tag matched. – alex Apr 22 '12 at 12:26
  • http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – AHM Apr 22 '12 at 12:29
  • @alex: yes could be a solution but fine with the regex of the accepted answer – user1104419 Apr 22 '12 at 12:47
  • @ahm: yes i saw this post but in my case im fine with the answer - is not a massive dangerous/very important application. thx anyway – user1104419 Apr 22 '12 at 12:48

1 Answers1

4

Use this regex:-

<(?!/?(p|br|ul|li|strike|em|strong|a)(>|\s))[^<]+?>

LIVE DEMO

For more information, refer to my earlier answer, which fullfill your requirement.

Community
  • 1
  • 1
Siva Charan
  • 17,940
  • 9
  • 60
  • 95