简体   繁体   中英

Decoding Special Character

I am making an api call which sometimes returns strings with special character codes in the string, something like adiós is being returned, and I need to display to my user as adiós instead. I have tried several combinations of escape / unescape encodeURI / decodeURI on their own and together, but no matter what I do I can't seem to get this to work! My teammates were able to get them to display using dangerouslySetInnerHTML , but I know there has to be a better way? What might I be missing on this? The only other thing I can think would be to write a regex for it, but the problem is I don't know all of the characters that I might get back from the api to know what to write in the regex. Any help would be appreciated, I have read a few posts, one of which was this one, and another that suggested setting utf-8 encoding in the index.html file, but the problem is that is already set. We are using react for our project if it makes any difference!

This is the code where I am setting the text

setHelperText(
  // todo: fix this to display dangerously set inner HTML so it handles regex
  `Sorry, wrong answer! The correct answer was: ${currentQuestion.correctAnswer}`
);

Where currentQuestion is from the api request. The api is https://opentdb.com/api

You could consider using a DOMParser to parse the HTML entities for you. The main reason why dangerouslySetInnerHTML is considered dangerous is if you're displaying user input back to the end-user as this can enable XSS attacks. You can also consider using DOMPurify or another sanitizing library if you're wanting to reflect user input back to the user.

Here is an example using a DOMParser to parse the HTML string:

 const parseEntities = txt => new DOMParser().parseFromString(txt, 'text/html').body.innerText; const App = () => { const txt = 'adi&oacute;s'; return <p>{parseEntities(txt)}</p>; }; ReactDOM.render(<App />, document.body);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Maybe this helps

 function htmlEntities(str) { return String(str).replace('&ntilde;', 'ñ').replace('&Ntilde;', 'Ñ').replace('&amp;', '&').replace('&Ntilde;', 'Ñ').replace('&ntilde;', 'ñ').replace('&Ntilde;', 'Ñ').replace('&Agrave;', 'À').replace('&Aacute;', 'Á').replace('&Acirc;','Â').replace('&Atilde;','Ã').replace('&Auml;','Ä').replace('&Aring;','Å').replace('&AElig;','Æ').replace('&Ccedil;','Ç').replace('&Egrave;','È').replace('&Eacute;','É').replace('&Ecirc;', 'Ê').replace('&Euml;','Ë').replace( '&Igrave;', 'Ì').replace('&Iacute;', 'Í' ).replace('&Icirc;', 'Î' ).replace( '&Iuml;', 'Ï').replace( '&ETH;', 'Ð').replace( '&Ntilde;', 'Ñ').replace( '&Ograve;', 'Ò').replace( '&Oacute;', 'Ó').replace('&Ocirc;', 'Ô' ).replace( '&Otilde;', 'Õ').replace('&Ouml;', 'Ö' ).replace('&Oslash;', 'Ø' ).replace( '&Ugrave;','Ù').replace( '&Uacute;', 'Ú').replace( '&Ucirc;', 'Û').replace( '&Uuml;', 'Ü').replace( '&Yacute;', 'Ý').replace('&THORN;', 'Þ' ).replace( '&szlig;', 'ß').replace( '&agrave;', 'à').replace( '&aacute;', 'á').replace( '&acirc;', 'â').replace( '&atilde;', 'ã').replace('&auml;', 'ä' ).replace( '&aring;', 'å').replace( '&aelig;', 'æ').replace( '&ccedil;', 'ç').replace('&egrave;', 'è' ).replace('&eacute;', 'é' ).replace('&ecirc;', 'ê' ).replace('&euml;', 'ë' ).replace( '&igrave;', 'ì').replace('&iacute;', 'í' ).replace('&icirc;', 'î' ).replace('&iuml;', 'ï' ).replace('&eth;', 'ð' ).replace( '&ntilde;', 'ñ').replace('&ograve;','ò').replace('&oacute;','ó').replace('&ocirc;','ô').replace('&otilde;','õ').replace('&ouml;','ö').replace('&oslash;','ø').replace('&ugrave;','ù').replace('&uacute;','ú').replace('&ucirc;','û').replace('&uuml;', 'ü').replace('&yacute;', 'ý').replace('&thorn;', 'þ').replace('&yuml;', 'ÿ'); } console.log(htmlEntities("adi&oacute;s"));

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