admin管理员组

文章数量:1335594

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

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

Share Improve this question edited Jun 25, 2010 at 7:10 Jibu P C_Adoor asked Jun 25, 2010 at 6:47 Jibu P C_AdoorJibu P C_Adoor 3,3749 gold badges32 silver badges36 bronze badges 4
  • How about &copy;, &#12345;, &#xabcd; and ordinal text like foo? Do you just want to strip HTML tags like <script>? <b>? – kennytm Commented Jun 25, 2010 at 6:54
  • No i want accept all the html tags but if it the content is in the encodeed format that should be avoided – Jibu P C_Adoor Commented Jun 25, 2010 at 6:56
  • So you want to reject the input if there's &lt;? – kennytm Commented Jun 25, 2010 at 7:01
  • exactly,but should not reject '<' or '>' – Jibu P C_Adoor Commented Jun 25, 2010 at 7:05
Add a ment  | 

2 Answers 2

Reset to default 11

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 ment:

   "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

本文标签: regexRegular expression in javascript for restricting HTML entites like ampltStack Overflow