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

4
  • How about &copy;, &#12345;, &#xabcd; and ordinal text like foo? Do you just want to strip HTML tags like <script>? <b>? 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 Commented Jun 25, 2010 at 6:56
  • So you want to reject the input if there's &lt;? Commented Jun 25, 2010 at 7:01
  • exactly,but should not reject '<' or '>' Commented Jun 25, 2010 at 7:05

2 Answers 2

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 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]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

You can write {0,1} as ?.
@Gumbo: True, but not my style.
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+.
0

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

1 Comment

It will replace the specified string to empty string, but i want to identify the specified string contains '&lt;'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.