Reputation: 383
I want to replace (remove) html tags outside the [code] bbcode using javascript. for example:
<script>these</script> [code]<script>alert</script>[/code]<script>that</script>
should become
these [code]<script>alert</script>[/code]that
how use RegEx to replace/remove tags outside [code]?
Upvotes: 1
Views: 273
Reputation: 435
Replace this /(\[code\][\s\S]*?\[\/code\])|<[\s\S]*?>/g
to $1
:
your_string.replace(/(\[code\][\s\S]*?\[\/code\])|<[\s\S]*?>/g, '$1');
It'll find all [code]
tags first, save them, and after that it will find the remaining html tags (which will not be in the [code]
tags).
Upvotes: 3
Reputation: 383
ok I find a solution:
replace(/<(?=[^\[]*\[\/code\])/gi,"&_lt_;");
replace(/>(?=[^\[]*\[\/code\])/gi,"&_gt_;");
DO OTHER REPLACEMENT/CUSTOMIZATION HERE
replace(/&_lt_;/gi,"<");
replace(/&_gt_;/gi,">");
that it! :)
Upvotes: -2