简体   繁体   中英

Convert unicode from Json in HTML with Javascript

I am getting issues to convert unicode and render in a nice HTML code.

Here is the information i have as an input in my Json file

`"CLIENT:\r\n-Client1: Project1\u00c2\u00a0\r\n- Client2: etc..."`

I would like this to render as below in

CLIENT: - client1: Project 1 - Client2: etc...

It currently renders like this :

`CLIENT: - Client1: Project1Â  - Client2: etc...`

I looked everywhere but could not find a function that could handle all unicodes to decode in nice html code. Thanks in advance!

Maybe you can take a look at this: How do I replace all line breaks in a string with
tags?

You do this:

str = str.replace(/(?:\r\n|\r|\n)/g, '<br />');

before insert into the html.

If you are using javascript, you can use this fragment

 <script> var c = decodeURIComponent(escape(`"CLIENT:\\r\\n-Client1: Project1\Â\ \\r\\n- Client2: etc..."`)); c = c.replace(/(?:\\r\\n|\\r|\\n)/g, ''); c = c.replace(':-', ': - '); document.write(c); </script>

Ok I managed to make this work with the below function:

function strFormat(str) {
    var c= decodeURIComponent(escape(str));
    c= c.replace(/(?:\r\n|\r|\n)/g, '<br />');
    c= c.replace(':-', ': - ');
    return c;

}

Let me know if there are any cleaner way to do this?

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