Ok, let say I got textarea
and user can input any sort of text into it.
Then I want to put this text into a div
element. For example,
document.getElementById('myDiv').innerHTML=text;
The issue is that user can put html code into it and it can distort the div
. However, the text can contain <b>
or <i>
.
So I want to replace all <
with <
& all >
with >
& except <b>
or <i>
.
Note that: space before and after i
are allowed so we will keep <i >
, < i>
, < i >
, etc. Also, <b>
/ </b>
& <i>
/ </i>
must go in pair. That means if there is a <b>
but there is no </b>
then it should escape <b>
& it should do the same with <i>
.
so, How to use Java Regex to sanitize html that accept only <b>
and <i>
tag?