views:

53

answers:

2

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

A: 

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

M3t0r
It will replace the specified string to empty string, but i want to identify the specified string contains '<'
Jibu P C_Adoor
+1  A: 

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]);
}
Brock Adams
You can write `{0,1}` as `?`.
Gumbo
@Gumbo: True, but not my style.
Brock Adams
@Brock Adams: Not your style? What is your style? You should always write your code as comprehensible as possible. Quantifiers like `?` and `+` were added for the same reason to not to be forced to write something like `(x|)` but simply `x?` and not `xx*` but simply `x+`.
Gumbo