Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression in javascript for restricting HTML entites like <

Hii,

I want to restrict the html entities like '&lt;' , "&gt;" , "&amp;"etc but it should accept '<' and '>' when i click on a button from the javascript. Can anybody gives me the regular expression for that

like image 835
Jibu P C_Adoor Avatar asked Jul 04 '26 17:07

Jibu P C_Adoor


1 Answers

Updated regex for all entities, including numeric...

Javascript like:

var StrippedStr = YourStrVar.replace (/&#{0,1}[a-z0-9]+;/ig, "");

will strip just about every non-numeric html entity.

.

UPDATE:

Based on comment:

   "but i want to identify the specified string contains &lt;"

.

You can test for entities with:

var HasEntity = /&#{0,1}[a-z0-9]+;/i. test (YourStrVar);

.

You can get a list of the entities with:

var ListOfEntities = YourStrVar.match (/&#{0,1}[a-z0-9]+;/ig);

for (var J=0;  J < ListOfEntities.length;  J++)
{
    alert ('Entity ' + (J+1) + '= ' + ListOfEntities[J]);
}
like image 155
Brock Adams Avatar answered Jul 06 '26 06:07

Brock Adams