Using escape() should work with the character code range 0x00 to 0xFF (UTF-8 range).
If you go beyond 0xFF (255), such as 0x100 (256) then escape() will not work:
escape("\u0100"); // %u0100
and:
text = "\u0100"; // A
html = escape(text).replace(/%(..)/g,"&#x$1;"); // &#xu0;100
So, if you want to cover all Unicode charachacters as defined on http://www.w3.org/TR/html4/sgml/entities.html , then you could use something like:
var html = text.replace(/[\u00A0-\u00FF]/g, function(c) {
return '&#'+c.charCodeAt(0)+';';
});
Note here the range is between: \u00A0-\u00FF.
Thats the first character code range defined in http://www.w3.org/TR/html4/sgml/entities.html which is the same as what escape() covers.
You'll need to add the other ranges you want to cover as well, or all of them.
Example: UTF-8 range with general punctuations (\u00A0-\u00FF and \u2022-\u2135)
var html = text.replace(/[\u00A0-\u00FF\u2022-\u2135]/g, function(c) {
return '&#'+c.charCodeAt(0)+';';
});
Edit:
BTW: \u00A0-\u2666 should convert every Unicode character code not within ASCII range to HTML entities blindly:
var html = text.replace(/[\u00A0-\u2666]/g, function(c) {
return '&#'+c.charCodeAt(0)+';';
});