0

I got   instead of space on my html textbox. I am trying to check if string is encoded or not i.e. 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;
}
1

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.