0

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.

4 Answers 4

4

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&#32;world&#33;&#32;&#169;&#8482;&#27721
Sign up to request clarification or add additional context in comments.

2 Comments

Your solution is robust, but &copy; is a lot more readable than &169; in case that matters to the asker.
nice Crozin thanks!, readability of the source didn't matter to me, but I guess it might to others.
2

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 &lt;, &gt;, &quot;, and &amp;.

Comments

1

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['"'] = '&quot;';
characters['\''] = '&apos;';
characters['&'] = '&amp;';
characters['<'] = '&lt;';
characters['>'] = '&gt;';
// ...

Then just look through each character in your text, and start replacing.

Comments

0

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:

"" &quot; quotation mark quotation mark &#34; quotation mark ' ' &quot; ' &#39; &#39; quotation mark &#39; &apos; (does not work in IE) &apos; (does not work in IE) ' &apos; (does not work in IE) apostrophe  apostrophe  &#39; apostrophe  & & &apos; (does not work in IE) & &#38; &#38; apostrophe  &#38; &amp; &amp; & &amp; ampersand ampersand &#38; ampersand < < &amp; < &#60; &#60; ampersand &#60; &lt; &lt; < &lt; less-than less-than &#60; less-than > > &lt; > &#62; &#62; less-than &#62; &gt; &gt; > &gt; greater-than greater-than &#62; greater-than     &gt;   &#160; &#160; greater-than &#160; &nbsp; &nbsp;   &nbsp; non-breaking space non-breaking space &#160; non-breaking space ¡ ¡ &nbsp; ¡ &#161; &#161; non-breaking space &#161; &iexcl; &iexcl; ¡ &iexcl; inverted exclamation mark inverted exclamation mark &#161; inverted exclamation mark ¢ ¢ &iexcl; ¢ &#162; &#162; inverted exclamation mark &#162; &cent; &cent; ¢ &cent; cent cent &#162; cent £ £ &cent; £ &#163; &#163; cent &#163; &pound; &pound; £ &pound; pound pound &#163; pound ¤ ¤ &pound; ¤ &#164; &#164; pound &#164; &curren; &curren; ¤ &curren; currency currency &#164; currency ¥ ¥ &curren; ¥ &#165; &#165; currency &#165; &yen; &yen; ¥ &yen; yen yen &#165; yen ¦ ¦ &yen; ¦ &#166; &#166; yen &#166; &brvbar; &brvbar; ¦ &brvbar; broken vertical bar broken vertical bar &#166; broken vertical bar § § &brvbar; § &#167; &#167; broken vertical bar &#167; &sect; &sect; § &sect; section section &#167; section ¨ ¨ &sect; ¨ &#168; &#168; section &#168; &uml; &uml; ¨ &uml; spacing diaeresis spacing diaeresis &#168; spacing diaeresis © © &uml; © &#169; &#169; spacing diaeresis &#169; &copy; &copy; © &copy; copyright copyright &#169; copyright ª ª &copy; ª &#170; &#170; copyright &#170; &ordf; &ordf; ª &ordf; feminine ordinal indicator feminine ordinal indicator &#170; feminine ordinal indicator « « &ordf; « &#171; &#171; feminine ordinal indicator &#171; &laquo; &laquo; « &laquo; angle quotation mark (left) angle quotation mark (left) &#171; angle quotation mark (left) ¬ ¬ &laquo; ¬ &#172; &#172; angle quotation mark (left) &#172; &not; &not; ¬ &not; negation negation &#172; negation ­ ­ &not; ­ &#173; &#173; negation &#173; &shy; &shy; ­ &shy; soft hyphen soft hyphen &#173; soft hyphen ® ® &shy; ® &#174; &#174; soft hyphen &#174; &reg; &reg; ® &reg; registered trademark registered trademark &#174; registered trademark ¯ ¯ &reg; ¯ &#175; &#175; registered trademark &#175; &macr; &macr; ¯ &macr; spacing macron spacing macron &#175; spacing macron ° ° &macr; ° &#176; &#176; spacing macron &#176; &deg; &deg; ° &deg; degree degree &#176; degree ± ± &deg; ± &#177; &#177; degree &#177; &plusmn; &plusmn; ± &plusmn; plus-or-minus  plus-or-minus  &#177; plus-or-minus  ² ² &plusmn; ² &#178; &#178; plus-or-minus  &#178; &sup2; &sup2; ² &sup2; superscript 2 superscript 2 &#178; superscript 2 ³ ³ &sup2; ³ &#179; &#179; superscript 2 &#179; &sup3; &sup3; ³ &sup3; superscript 3 superscript 3 &#179; superscript 3 ´ ´ &sup3; ´ &#180; &#180; superscript 3 &#180; &acute; &acute; ´ &acute; spacing acute spacing acute 

Shouldn't be too hard to mold this to your specific needs

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.