2

I'm looking to convert a string of html entities specifying ASCII codes (ie: a) to the ASCII characters they represent (ie: a). I'm using a property of an object and trying to assign a value. For instance:

object.Text("");

When I pass is the string representing the entity, I get the same string back. I can't find the function to convert entities to the characters they represented.

3
  • The string "a" is an HTML character entity. Do you mean to ask how to HTML-decode a string? That's not really what you've asked. Commented Feb 9, 2009 at 15:47
  • Ugh. Both the title and the text are poorly worded. Commented Feb 9, 2009 at 15:51
  • OK, I took a wack at it. Is it better this way? Commented Feb 9, 2009 at 15:57

3 Answers 3

22

To convert all numerical character entities in a string to their character equivalents you can do this:

str.replace(/&#(\d+);/g, function (m, n) { return String.fromCharCode(n); })
Sign up to request clarification or add additional context in comments.

1 Comment

I really like this solution, but do keep in mind that while   will be converted,   and other non-numeric escape codes will not.
11

Try the String.fromCharCode() function.

alert(String.fromCharCode(97));

As you can see, you'll have to strip out the ampersand and pound sign.

Best regards...

Comments

1

Check String.fromCharCode.

Comments

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.