偶尔会在数据中看到诸如' 这样的字符,浏览器遇到这些转义符,会转义回来,但如何通过代码识别,下面就与大家探讨下html转义字符,喜欢的朋友可以了解下
偶尔会在数据中看到诸如' 这样的字符,特征如下
以开头,中间是一串数字,以;结尾
以&开头,中间一串字符,以;结尾
比如最常见的 或者等价的
浏览器遇到这些转义符,会转义回来,但如何通过代码识别? org.apache.commons.lang.stringescapeutils.unescapehtml提供了很好的说明
遇到上面的第一种情况,中间是数字的,直接将数字(unicode)转为char
遇到第二情况,中间是字符,只能查映射表了,从映射表中找到字符对应的数字再转换为char 看看代码就一目了然了
看看html40如何定义的
代码如下:
static {
html40 = new entities();
fillwithhtml40entities(html40);
}
static void fillwithhtml40entities(entities entities) {
entities.addentities(basic_array);
entities.addentities(iso8859_1_array);
entities.addentities(html40_array);
}
再看看basic_array、iso8859_1_array、html40_array 分别是什么
basic_array
代码如下:
private static final string[][] basic_array = {{"quot", "34"}, // " - double-quote
{"amp", "38"}, // & - ampersand
{"lt", "60"}, // < - less-than
{"gt", "62"}, // > - greater-than
};
iso8859_1_array
代码如下:
static final string[][] iso8859_1_array = {{"nbsp", "160"}, // non-breaking space
{"iexcl", "161"}, // inverted exclamation mark
{"cent", "162"}, // cent sign
{"pound", "163"}, // pound sign
{"curren", "164"}, // currency sign
{"yen", "165"}, // yen sign = yuan sign
{"brvbar", "166"}, // broken bar = broken vertical bar
{"sect", "167"}, // section sign
{"uml", "168"}, // diaeresis = spacing diaeresis
{"copy", "169"}, // � - copyright sign
{"ordf", "170"}, // feminine ordinal indicator
{"laquo", "171"}, // left-pointing double angle quotation mark = left pointing guillemet
{"not", "172"}, // not sign
{"shy", "173"}, // soft hyphen = discretionary hyphen
{"reg", "174"}, // � - registered trademark sign
{"macr", "175"}, // macron = spacing macron = overline = apl overbar
{"deg", "176"}, // degree sign
{"plusmn", "177"}, // plus-minus sign = plus-or-minus sign
{"sup2", "178"}, // superscript two = superscript digit two = squared
{"sup3", "179"}, // superscript three = superscript digit three = cubed
{"acute", "180"}, // acute accent = spacing acute
{"micro", "181"}, // micro sign
{"para", "182"}, // pilcrow sign = paragraph sign
{"middot", "183"}, // middle dot = georgian comma = greek middle dot
{"cedil", "184"}, // cedilla = spacing cedilla
{"sup1", "185"}, // superscript one = superscript digit one
{"ordm", "186"}, // masculine ordinal indicator
{"raquo", "187"}, // right-pointing double angle quotation mark = right pointing guillemet
{"frac14", "188"}, // vulgar fraction one quarter = fraction one quarter
{"frac12", "189"}, // vulgar fraction one half = fraction one half
{"frac34", "190"}, // vulgar fraction three quarters = fraction three quarters
{"iquest", "191"}, // inverted question mark = turned question mark
{"agrave", "192"}, // � - uppercase a, grave accent
{"aacute", "193"}, // � - uppercase a, acute accent
{"acirc", "194"}, // � - uppercase a, circumflex accent
{"atilde", "195"}, // � - uppercase a, tilde
{"auml", "196"}, // � - uppercase a, umlaut
{"aring", "197"}, // � - uppercase a, ring
{"aelig", "198"}, // � - uppercase ae
{"ccedil", "199"}, // � - uppercase c, cedilla
{"egrave", "200"}, // � - uppercase e, grave accent
{"eacute", "201"}, // � - uppercase e, acute accent
{"ecirc", "202"}, // � - uppercase e, circumflex accent
{"euml", "203"}, // � - uppercase e, umlaut
{"igrave", "204"}, // � - uppercase i, grave accent
{"iacute", "205"}, // � - uppercase i, acute accent
{"icirc", "206"}, // � - uppercase i, circumflex accent
{"iuml", "207"}, // � - uppercase i, umlaut
{"eth", "208"}, // � - uppercase eth, icelandic
{"ntilde", "209"}, // � - uppercase n, tilde
{"ograve", "210"}, // � - uppercase o, grave accent
{"oacute", "211"}, // � - uppercase o, acute accent
{"ocirc", "212"}, // � - uppercase o, circumflex accent
{"otilde", "213"}, // � - uppercase o, tilde
{"ouml", "214"}, // � - uppercase o, umlaut
{"times", "215"}, // multiplication sign
{"oslash", "216"}, // � - uppercase o, slash
{"ugrave", "217"}, // � - uppercase u, grave accent
{"uacute", "218"}, // � - uppercase u, acute accent
{"ucirc", "219"}, // � - uppercase u, circumflex accent
{"uuml", "220"}, // � - uppercase u, umlaut
{"yacute", "221"}, // � - uppercase y, acute accent
{"thorn", "222"}, // � - uppercase thorn, icelandic
{"szlig", "223"}, // � - lowercase sharps, german
{"agrave", "224"}, // � - lowercase a, grave accent
{"aacute", "225"}, // � - lowercase a, acute accent
{"acirc", "226"}, // � - lowercase a, circumflex accent
{"atilde", "227"}, // � - lowercase a, tilde
{"auml", "228"}, // � - lowercase a, umlaut
{"aring", "229"}, // � - lowercase a, ring
{"aelig", "230"}, // � - lowercase ae
{"ccedil", "231"}, // � - lowercase c, cedilla
{"egrave", "232"}, // � - lowercase e, grave accent
{"eacute", "233"}, // � - lowercase e, acute accent
{"ecirc", "234"}, // � - lowercase e, circumflex accent
{"euml", "235"}, // � - lowercase e, umlaut
{"igrave", "236"}, // � - lowercase i, grave accent
{"iacute", "237"}, // � - lowercase i, acute accent
{"icirc", "238"}, // � - lowercase i, circumflex accent
{"iuml", "239"}, // � - lowercase i, umlaut
{"eth", "240"}, // � - lowercase eth, icelandic
{"ntilde", "241"}, // � - lowercase n, tilde
{"ograve", "242"}, // � - lowercase o, grave accent
{"oacute", "243"}, // � - lowercase o, acute accent
{"ocirc", "244"}, // � - lowercase o, circumflex accent
{"otilde", "245"}, // � - lowercase o, tilde
{"ouml", "246"}, // � - lowercase o, umlaut
{"pide", "247"}, // pision sign
{"oslash", "248"}, // � - lowercase o, slash
{"ugrave", "249"}, // � - lowercase u, grave accent
{"uacute", "250"}, // � - lowercase u, acute accent
{"ucirc", "251"}, // � - lowercase u, circumflex accent
{"uuml", "252"}, // � - lowercase u, umlaut
{"yacute", "253"}, // � - lowercase y, acute accent
{"thorn", "254"}, // � - lowercase thorn, icelandic
{"yuml", "255"}, // � - lowercase y, umlaut
};
html40_array
代码如下:
static final string[][] html40_array = {
// <!-- latin extended-b -->
{"fnof", "402"}, // latin small f with hook = function= florin, u+0192 isotech -->
// <!-- greek -->
{"alpha", "913"}, // greek capital letter alpha, u+0391 -->
{"beta", "914"}, // greek capital letter beta, u+0392 -->
{"gamma", "915"}, // greek capital letter gamma,u+0393 isogrk3 -->
{"delta", "916"}, // greek capital letter delta,u+0394 isogrk3 -->
{"epsilon", "917"}, // greek capital letter epsilon, u+0395 -->
{"zeta", "918"}, // greek capital letter zeta, u+0396 -->
{"eta", "919"}, // greek capital letter eta, u+0397 -->
{"theta", "920"}, // greek capital letter theta,u+0398 isogrk3 -->
{"iota", "921"}, // greek capital letter iota, u+0399 -->
{"kappa", "922"}, // greek capital letter kappa, u+039a -->
{"lambda", "923"}, // greek capital letter lambda,u+039b isogrk3 -->
{"mu", "924"}, // greek capital letter mu, u+039c -->
{"nu", "925"}, // greek capital letter nu, u+039d -->
{"xi", "926"}, // greek capital letter xi, u+039e isogrk3 -->
{"omicron", "927"}, // greek capital letter omicron, u+039f -->
{"pi", "928"}, // greek capital letter pi, u+03a0 isogrk3 -->
{"rho", "929"}, // greek capital letter rho, u+03a1 -->
// <!-- there is no sigmaf, and no u+03a2 character either -->
{"sigma", "931"}, // greek capital letter sigma,u+03a3 isogrk3 -->
{"tau", "932"}, // greek capital letter tau, u+03a4 -->
{"upsilon", "933"}, // greek capital letter upsilon,u+03a5 isogrk3 -->
{"phi", "934"}, // greek capital letter phi,u+03a6 isogrk3 -->
{"chi", "935"}, // greek capital letter chi, u+03a7 -->
{"psi", "936"}, // greek capital letter psi,u+03a8 isogrk3 -->
{"omega", "937"}, // greek capital letter omega,u+03a9 isogrk3 -->
{"alpha", "945"}, // greek small letter alpha,u+03b1 isogrk3 -->
{"beta", "946"}, // greek small letter beta, u+03b2 isogrk3 -->
{"gamma", "947"}, // greek small letter gamma,u+03b3 isogrk3 -->
{"delta", "948"}, // greek small letter delta,u+03b4 isogrk3 -->
{"epsilon", "949"}, // greek small letter epsilon,u+03b5 isogrk3 -->
{"zeta", "950"}, // greek small letter zeta, u+03b6 isogrk3 -->
{"eta", "951"}, // greek small letter eta, u+03b7 isogrk3 -->
{"theta", "952"}, // greek small letter theta,u+03b8 isogrk3 -->
{"iota", "953"}, // greek small letter iota, u+03b9 isogrk3 -->
{"kappa", "954"}, // greek small letter kappa,u+03ba isogrk3 -->
{"lambda", "955"}, // greek small letter lambda,u+03bb isogrk3 -->
{"mu", "956"}, // greek small letter mu, u+03bc isogrk3 -->
{"nu", "957"}, // greek small letter nu, u+03bd isogrk3 -->
{"xi", "958"}, // greek small letter xi, u+03be isogrk3 -->
{"omicron", "959"}, // greek small letter omicron, u+03bf new -->
{"pi", "960"}, // greek small letter pi, u+03c0 isogrk3 -->
{"rho", "961"}, // greek small letter rho, u+03c1 isogrk3 -->
{"sigmaf", "962"}, // greek small letter final sigma,u+03c2 isogrk3 -->
{"sigma", "963"}, // greek small letter sigma,u+03c3 isogrk3 -->
{"tau", "964"}, // greek small letter tau, u+03c4 isogrk3 -->
{"upsilon", "965"}, // greek small letter upsilon,u+03c5 isogrk3 -->
{"phi", "966"}, // greek small letter phi, u+03c6 isogrk3 -->
{"chi", "967"}, // greek small letter chi, u+03c7 isogrk3 -->
{"psi", "968"}, // greek small letter psi, u+03c8 isogrk3 -->
{"omega", "969"}, // greek small letter omega,u+03c9 isogrk3 -->
{"thetasym", "977"}, // greek small letter theta symbol,u+03d1 new -->
{"upsih", "978"}, // greek upsilon with hook symbol,u+03d2 new -->
{"piv", "982"}, // greek pi symbol, u+03d6 isogrk3 -->
// <!-- general punctuation -->
{"bull", "8226"}, // bullet = black small circle,u+2022 isopub -->
// <!-- bullet is not the same as bullet operator, u+2219 -->
{"hellip", "8230"}, // horizontal ellipsis = three dot leader,u+2026 isopub -->
{"prime", "8242"}, // prime = minutes = feet, u+2032 isotech -->
{"prime", "8243"}, // double prime = seconds = inches,u+2033 isotech -->
{"oline", "8254"}, // overline = spacing overscore,u+203e new -->
{"frasl", "8260"}, // fraction slash, u+2044 new -->
// <!-- letterlike symbols -->
{"weierp", "8472"}, // script capital p = power set= weierstrass p, u+2118 isoamso -->
{"image", "8465"}, // blackletter capital i = imaginary part,u+2111 isoamso -->
{"real", "8476"}, // blackletter capital r = real part symbol,u+211c isoamso -->
{"trade", "8482"}, // trade mark sign, u+2122 isonum -->
{"alefsym", "8501"}, // alef symbol = first transfinite cardinal,u+2135 new -->
// <!-- alef symbol is not the same as hebrew letter alef,u+05d0 although the
// same glyph could be used to depict both characters -->
// <!-- arrows -->
{"larr", "8592"}, // leftwards arrow, u+2190 isonum -->
{"uarr", "8593"}, // upwards arrow, u+2191 isonum-->
{"rarr", "8594"}, // rightwards arrow, u+2192 isonum -->
{"darr", "8595"}, // downwards arrow, u+2193 isonum -->
{"harr", "8596"}, // left right arrow, u+2194 isoamsa -->
{"crarr", "8629"}, // downwards arrow with corner leftwards= carriage return, u+21b5 new -->
{"larr", "8656"}, // leftwards double arrow, u+21d0 isotech -->
// <!-- iso 10646 does not say that larr is the same as the 'is implied by'
// arrow but also does not have any other character for that function.
// so ? larr canbe used for 'is implied by' as isotech suggests -->
{"uarr", "8657"}, // upwards double arrow, u+21d1 isoamsa -->
{"rarr", "8658"}, // rightwards double arrow,u+21d2 isotech -->
// <!-- iso 10646 does not say this is the 'implies' character but does not
// have another character with this function so ?rarr can be used for
// 'implies' as isotech suggests -->
{"darr", "8659"}, // downwards double arrow, u+21d3 isoamsa -->
{"harr", "8660"}, // left right double arrow,u+21d4 isoamsa -->
// <!-- mathematical operators -->
{"forall", "8704"}, // for all, u+2200 isotech -->
{"part", "8706"}, // partial differential, u+2202 isotech -->
{"exist", "8707"}, // there exists, u+2203 isotech -->
{"empty", "8709"}, // empty set = null set = diameter,u+2205 isoamso -->
{"nabla", "8711"}, // nabla = backward difference,u+2207 isotech -->
{"isin", "8712"}, // element of, u+2208 isotech -->
{"notin", "8713"}, // not an element of, u+2209 isotech -->
{"ni", "8715"}, // contains as member, u+220b isotech -->
// <!-- should there be a more memorable name than 'ni'? -->
{"prod", "8719"}, // n-ary product = product sign,u+220f isoamsb -->
// <!-- prod is not the same character as u+03a0 'greek capital letter pi'
// though the same glyph might be used for both -->
{"sum", "8721"}, // n-ary summation, u+2211 isoamsb -->
// <!-- sum is not the same character as u+03a3 'greek capital letter sigma'
// though the same glyph might be used for both -->
{"minus", "8722"}, // minus sign, u+2212 isotech -->
{"lowast", "8727"}, // asterisk operator, u+2217 isotech -->
{"radic", "8730"}, // square root = radical sign,u+221a isotech -->
{"prop", "8733"}, // proportional to, u+221d isotech -->
{"infin", "8734"}, // infinity, u+221e isotech -->
{"ang", "8736"}, // angle, u+2220 isoamso -->
{"and", "8743"}, // logical and = wedge, u+2227 isotech -->
{"or", "8744"}, // logical or = vee, u+2228 isotech -->
{"cap", "8745"}, // intersection = cap, u+2229 isotech -->
{"cup", "8746"}, // union = cup, u+222a isotech -->
{"int", "8747"}, // integral, u+222b isotech -->
{"there4", "8756"}, // therefore, u+2234 isotech -->
{"sim", "8764"}, // tilde operator = varies with = similar to,u+223c isotech -->
// <!-- tilde operator is not the same character as the tilde, u+007e,although
// the same glyph might be used to represent both -->
{"cong", "8773"}, // approximately equal to, u+2245 isotech -->
{"asymp", "8776"}, // almost equal to = asymptotic to,u+2248 isoamsr -->
{"ne", "8800"}, // not equal to, u+2260 isotech -->
{"equiv", "8801"}, // identical to, u+2261 isotech -->
{"le", "8804"}, // less-than or equal to, u+2264 isotech -->
{"ge", "8805"}, // greater-than or equal to,u+2265 isotech -->
{"sub", "8834"}, // subset of, u+2282 isotech -->
{"sup", "8835"}, // superset of, u+2283 isotech -->
// <!-- note that nsup, 'not a superset of, u+2283' is not covered by the
// symbol font encoding and is not included. should it be, for symmetry?
// it is in isoamsn --> <!entity nsub", "8836"},
// not a subset of, u+2284 isoamsn -->
{"sube", "8838"}, // subset of or equal to, u+2286 isotech -->
{"supe", "8839"}, // superset of or equal to,u+2287 isotech -->
{"oplus", "8853"}, // circled plus = direct sum,u+2295 isoamsb -->
{"otimes", "8855"}, // circled times = vector product,u+2297 isoamsb -->
{"perp", "8869"}, // up tack = orthogonal to = perpendicular,u+22a5 isotech -->
{"sdot", "8901"}, // dot operator, u+22c5 isoamsb -->
// <!-- dot operator is not the same character as u+00b7 middle dot -->
// <!-- miscellaneous technical -->
{"lceil", "8968"}, // left ceiling = apl upstile,u+2308 isoamsc -->
{"rceil", "8969"}, // right ceiling, u+2309 isoamsc -->
{"lfloor", "8970"}, // left floor = apl downstile,u+230a isoamsc -->
{"rfloor", "8971"}, // right floor, u+230b isoamsc -->
{"lang", "9001"}, // left-pointing angle bracket = bra,u+2329 isotech -->
// <!-- lang is not the same character as u+003c 'less than' or u+2039 'single left-pointing angle quotation
// mark' -->
{"rang", "9002"}, // right-pointing angle bracket = ket,u+232a isotech -->
// <!-- rang is not the same character as u+003e 'greater than' or u+203a
// 'single right-pointing angle quotation mark' -->
// <!-- geometric shapes -->
{"loz", "9674"}, // lozenge, u+25ca isopub -->
// <!-- miscellaneous symbols -->
{"spades", "9824"}, // black spade suit, u+2660 isopub -->
// <!-- black here seems to mean filled as opposed to hollow -->
{"clubs", "9827"}, // black club suit = shamrock,u+2663 isopub -->
{"hearts", "9829"}, // black heart suit = valentine,u+2665 isopub -->
{"diams", "9830"}, // black diamond suit, u+2666 isopub -->
// <!-- latin extended-a -->
{"oelig", "338"}, // -- latin capital ligature oe,u+0152 isolat2 -->
{"oelig", "339"}, // -- latin small ligature oe, u+0153 isolat2 -->
// <!-- ligature is a misnomer, this is a separate character in some languages -->
{"scaron", "352"}, // -- latin capital letter s with caron,u+0160 isolat2 -->
{"scaron", "353"}, // -- latin small letter s with caron,u+0161 isolat2 -->
{"yuml", "376"}, // -- latin capital letter y with diaeresis,u+0178 isolat2 -->
// <!-- spacing modifier letters -->
{"circ", "710"}, // -- modifier letter circumflex accent,u+02c6 isopub -->
{"tilde", "732"}, // small tilde, u+02dc isodia -->
// <!-- general punctuation -->
{"ensp", "8194"}, // en space, u+2002 isopub -->
{"emsp", "8195"}, // em space, u+2003 isopub -->
{"thinsp", "8201"}, // thin space, u+2009 isopub -->
{"zwnj", "8204"}, // zero width non-joiner,u+200c new rfc 2070 -->
{"zwj", "8205"}, // zero width joiner, u+200d new rfc 2070 -->
{"lrm", "8206"}, // left-to-right mark, u+200e new rfc 2070 -->
{"rlm", "8207"}, // right-to-left mark, u+200f new rfc 2070 -->
{"ndash", "8211"}, // en dash, u+2013 isopub -->
{"mdash", "8212"}, // em dash, u+2014 isopub -->
{"lsquo", "8216"}, // left single quotation mark,u+2018 isonum -->
{"rsquo", "8217"}, // right single quotation mark,u+2019 isonum -->
{"sbquo", "8218"}, // single low-9 quotation mark, u+201a new -->
{"ldquo", "8220"}, // left double quotation mark,u+201c isonum -->
{"rdquo", "8221"}, // right double quotation mark,u+201d isonum -->
{"bdquo", "8222"}, // double low-9 quotation mark, u+201e new -->
{"dagger", "8224"}, // dagger, u+2020 isopub -->
{"dagger", "8225"}, // double dagger, u+2021 isopub -->
{"permil", "8240"}, // per mille sign, u+2030 isotech -->
{"lsaquo", "8249"}, // single left-pointing angle quotation mark,u+2039 iso proposed -->
// <!-- lsaquo is proposed but not yet iso standardized -->
{"rsaquo", "8250"}, // single right-pointing angle quotation mark,u+203a iso proposed -->
// <!-- rsaquo is proposed but not yet iso standardized -->
{"euro", "8364"}, // -- euro sign, u+20ac new -->
};
再扩展下
从前面可以看到转义字符中间的那段数字是unicode,那么 这个 转移字符可以 随便构造了 ,并不限于上面的定义,比如 中的unicode是20013,那么构造一个转移字符中,经过浏览器的渲染 就变回中了 ,虽然不必这么绕为了显示一个字符,但如果在一些不方便传输特殊字符的场景 就可以派上用途了
以上就是html转义字符如何通过代码识别方法的详细内容。