m3hdi
m3hdi

Reputation: 383

replace using javascript regex outside a special tag?

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

Answers (2)

daftcoder
daftcoder

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

m3hdi
m3hdi

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

Related Questions