I'm trying to use JavaScript & regex to replace numerical HTML entities with their actual Unicode characters, e.g.
foo's bar
→
foo's bar
This is what I got so far:
"foo's bar".replace(/&#([^\s]*);/g, "$1"); // "foo39s bar"
All that's left to do is to replace the number with String.fromCharCode($1)
, but I can't seem to get it to work. How can I do this?
"foo's bar".replace(/&#(\d+);/g, function(match, match2) {return String.fromCharCode(+match2);})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With