简体   繁体   中英

Check if string is encode or not with javascript

I got   instead of space on my html textbox. I am trying to check if string is encoded or not ie if it is a space or a   How can I check if a string is a real string to  

I tried this but no luck

Var isItaString = isEncoded(‘ &nbsp’)
function isEncoded(str) {
    return typeof str == "string" && decodeURIComponent(str) !== str;
}

Well this is not text encoding but conversion to HTML Entities.

To check you should see if there is any presence of HTML entity. Here is complete list of such words:

["&nbsp","&iexcl","&cent","&pound","&curren","&yen","&brvbar","&sect","&uml","&copy","&ordf","&laquo","&not","&reg","&macr","&deg","&plusmn","&sup2","&sup3","&acute","&micro","&para","&middot","&cedil","&sup1","&ordm","&raquo","&frac14","&frac12","&frac34","&iquest","&Agrave","&Aacute","&Acirc","&Atilde","&Auml","&Aring","&AElig","&Ccedil","&Egrave","&Eacute","&Ecirc","&Euml","&Igrave","&Iacute","&Icirc","&Iuml","&ETH","&Ntilde","&Ograve","&Oacute","&Ocirc","&Otilde","&Ouml","&times","&Oslash","&Ugrave","&Uacute","&Ucirc","&Uuml","&Yacute","&THORN","&szlig","&agrave","&aacute","&acirc","&atilde","&auml","&aring","&aelig","&ccedil","&egrave","&eacute","&ecirc","&euml","&igrave","&iacute","&icirc","&iuml","&eth","&ntilde","&ograve","&oacute","&ocirc","&otilde","&ouml","&divide","&oslash","&ugrave","&uacute","&ucirc","&uuml","&yacute","&thorn","&yuml","&fnof","&Alpha","&Beta","&Gamma","&Delta","&Epsilon","&Zeta","&Eta","&Theta","&Iota","&Kappa","&Lambda","&Mu","&Nu","&Xi","&Omicron","&Pi","&Rho","&Sigma","&Tau","&Upsilon","&Phi","&Chi","&Psi","&Omega","&alpha","&beta","&gamma","&delta","&epsilon","&zeta","&eta","&theta","&iota","&kappa","&lambda","&mu","&nu","&xi","&omicron","&pi","&rho","&sigmaf","&sigma","&tau","&upsilon","&phi","&chi","&psi","&omega","&thetasym","&upsih","&piv","&bull","&hellip","&prime","&Prime","&oline","&frasl","&weierp","&image","&real","&trade","&alefsym","&larr","&uarr","&rarr","&darr","&harr","&crarr","&lArr","&uArr","&rArr","&dArr","&hArr","&forall","&part","&exist","&empty","&nabla","&isin","&notin","&ni","&prod","&sum","&minus","&lowast","&radic","&prop","&infin","&ang","&and","&or","&cap","&cup","&int","&there4","&sim","&cong","&asymp","&ne","&equiv","&le","&ge","&sub","&sup","&nsub","&sube","&supe","&oplus","&otimes","&perp","&sdot","&lceil","&rceil","&lfloor","&rfloor","&lang","&rang","&loz","&spades","&clubs","&hearts","&diams","&\"","&amp","&lt","&gt","&OElig","&oelig","&Scaron","&scaron","&Yuml","&circ","&tilde","&ndash","&mdash","&lsquo","&rsquo","&sbquo","&ldquo","&rdquo","&bdquo","&dagger","&Dagger","&permil","&lsaquo","&rsaquo","&euro"]

Do a regex search in the string and you will be good!

Based on the help from answers I figured out that they are html entities and need regex, replacement.

var decodeEntities = (function decodeEntities() {
            var element = document.createElement('div');

            function decodeHTMLEntities(str) {
                if (str && typeof str === 'string') {
                    str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '');
                    str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, '');
                    element.innerHTML = str;
                    str = element.textContent;
                    element.textContent = '';
                }

                return str;
            }

            return decodeHTMLEntities;
        })();

It would be helpful to other if someone convert this code to a single function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM