最简单的 JavaScript HTMLEncode lib/函数实现是什么?

发布于 2024-10-12 14:23:17 字数 89 浏览 3 评论 0原文

我正在寻找一个 js 函数或库,可以将 等特殊字符转换为 ,有人知道吗?我正在寻找我能找到的最简单的一个。

I'm looking 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心房的律动 2024-10-19 14:23:17

这些是 HTML 命名实体,它们不是最佳解决方案— 更好地使用数字实体。为什么数字实体更好?因为您没有像 ©© 这样的地图。您所需要的只是一个角色本身。

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! ©™汉

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! ©™汉
再见回来 2024-10-19 14:23:17

我不知道有什么库或函数可以做到这一点,但如果您专门处理 Unicode,则不必对 <> 之外的任何特殊字符进行编码;"&

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 &.

蓝戈者 2024-10-19 14:23:17

实际上您所需要的只是一个查找表。这里有一个很好的 HTML 实体列表:

http://www.w3schools.com/tags/ref_entities .asp

您的查找代码将如下所示:

var characters = [];
characters['"'] = '"';
characters['\''] = ''';
characters['&'] = '&';
characters['<'] = '<';
characters['>'] = '>';
// ...

然后只需查看文本中的每个字符,然后开始替换。

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.

北座城市 2024-10-19 14:23:17

这就是我所做的。我去了 这里 并在 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;

并得到了这个:

"" " 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 

不应该太很难根据您的具体需求进行塑造

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文