I'm lo开发者_如何学运维oking for a js function or lib that'll convert special chars like ™
to ™
, does anyone know of any? I'm looking for the simplest one that I can find.
Those are HTML named entities and they're not a best solution — better use a numerical entities. Why numerical entities are better? Cause you don't have any map like ©
→ ©
. All you need is a character itself.
function abc(input) {
var output = "";
var allowedChars = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
for (var i = 0; i < input.length; i++) {
var char = input.charAt(i);
var charCode = input.charCodeAt(i);
if (isNaN(charCode)) {
continue;
}
if (allowedChars.indexOf(char) > -1) {
output += char;
} else {
output += "&#" + charCode + ";";
}
}
return output;
}
alert(abc("Hello world! ©™汉")); // Hello world! ©™汉
I don't know of a library or function for that, but if you deal exclusively with Unicode, you shouldn't have to encode any special characters beyond <
, >
, "
, and &
.
Really all you need is a lookup table. There's a nice big list of HTML entities here:
http://www.w3schools.com/tags/ref_entities.asp
Your lookup code would look like this:
var characters = [];
characters['"'] = '"';
characters['\''] = ''';
characters['&'] = '&';
characters['<'] = '<';
characters['>'] = '>';
// ...
Then just look through each character in your text, and start replacing.
Here's what I did. I went here and did this code in firebug:
ret = ''; td = jQuery('table.reference td'); for (var i = 0; i < (td.length / 4); i++) ret += td.eq(i).text() + ' ' + td.eq(i + 2).text() + ' ' + td.eq(i + 3).text() + ' ' + td.eq(i + 3).text()+ '\n'; ret;
and got this:
"" " quotation mark quotation mark " quotation mark ' ' " ' ' ' quotation mark ' ' (does not work in IE) ' (does not work in IE) ' ' (does not work in IE) apostrophe apostrophe ' apostrophe & & ' (does not work in IE) & & & apostrophe & & & & & ampersand ampersand & ampersand < < & < < < ampersand < < < < < less-than less-than < less-than > > < > > > less-than > > > > > greater-than greater-than > greater-than >     greater-than   non-breaking space non-breaking space   non-breaking space ¡ ¡ ¡ ¡ ¡ non-breaking space ¡ ¡ ¡ ¡ ¡ inverted exclamation mark inverted exclamation mark ¡ inverted exclamation mark ¢ ¢ ¡ ¢ ¢ ¢ inverted exclamation mark ¢ ¢ ¢ ¢ ¢ cent cent ¢ cent £ £ ¢ £ £ £ cent £ £ £ £ £ pound pound £ pound ¤ ¤ £ ¤ ¤ ¤ pound ¤ ¤ ¤ ¤ ¤ currency currency ¤ currency ¥ ¥ ¤ ¥ ¥ ¥ currency ¥ ¥ ¥ ¥ ¥ yen yen ¥ yen ¦ ¦ ¥ ¦ ¦ ¦ yen ¦ ¦ ¦ ¦ ¦ broken vertical bar broken vertical bar ¦ broken vertical bar § § ¦ § § § broken vertical bar § § § § § section section § section ¨ ¨ § ¨ ¨ ¨ section ¨ ¨ ¨ ¨ ¨ spacing diaeresis spacing diaeresis ¨ spacing diaeresis © © ¨ © © © spacing diaeresis © © © © © copyright copyright © copyright ª ª © ª ª ª copyright ª ª ª ª ª feminine ordinal indicator feminine ordinal indicator ª feminine ordinal indicator « « ª « « « feminine ordinal indicator « « « « « angle quotation mark (left) angle quotation mark (left) « angle quotation mark (left) ¬ ¬ « ¬ ¬ ¬ angle quotation mark (left) ¬ ¬ ¬ ¬ ¬ negation negation ¬ negation ¬ ­ ­ negation ­ ­ ­ ­ soft hyphen soft hyphen ­ soft hyphen ® ® ­ ® ® ® soft hyphen ® ® ® ® ® registered trademark registered trademark ® registered trademark ¯ ¯ ® ¯ ¯ ¯ registered trademark ¯ ¯ ¯ ¯ ¯ spacing macron spacing macron ¯ spacing macron ° ° ¯ ° ° ° spacing macron ° ° ° ° ° degree degree ° degree ± ± ° ± ± ± degree ± ± ± ± ± plus-or-minus plus-or-minus ± plus-or-minus ² ² ± ² ² ² plus-or-minus ² ² ² ² ² superscript 2 superscript 2 ² superscript 2 ³ ³ ² ³ ³ ³ superscript 2 ³ ³ ³ ³ ³ superscript 3 superscript 3 ³ superscript 3 ´ ´ ³ ´ ´ ´ superscript 3 ´ ´ ´ ´ ´ spacing acute spacing acute
Shouldn't be too hard to mold this to your specific needs
精彩评论