Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert escaped html ASCII codes to plain text using JavaScript

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.

like image 529
Jason N. Gaylord Avatar asked Mar 22 '26 18:03

Jason N. Gaylord


1 Answers

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); })
like image 130
Ates Goral Avatar answered Mar 24 '26 08:03

Ates Goral



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!