\' when i click on a button" />
开发者

Regular expression in javascript for restricting HTML entites like <

开发者 https://www.devze.com 2023-01-04 13:29 出处:网络
Hii, I want to restrict the html entities like \'&lt;\' , \"&gt;\" , \"&amp;\"etc but it should ac开发者_JS百科cept \'<\' and \'>\' when i click on a button

Hii,

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


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]);
}


maybe that?

function remove() {
  var buffer = document.getElementById("parent").innerHTML;
  buffer = buffer.replace(/'&lt;'/, "");
  buffer = buffer.replace(/'&gt;'/, "");
  buffer = buffer.replace(/'&amp;'/, "");
  document.getElementById("parent").innerHTML = buffer;
}

just adujst the id

0

精彩评论

暂无评论...
验证码 换一张
取 消