Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use JavaScript regex to replace numerical HTML entities with their actual characters

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?

like image 832
alfonso Avatar asked Nov 27 '10 15:11

alfonso


1 Answers

"foo's bar".replace(/&#(\d+);/g, function(match, match2) {return String.fromCharCode(+match2);})
like image 54
Ilia G Avatar answered Sep 30 '22 15:09

Ilia G