suiram85
suiram85

Reputation: 19

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!

Upvotes: 1

Views: 535

Answers (3)

suiram85
suiram85

Reputation: 19

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?

Upvotes: 0

Jose Bernalte
Jose Bernalte

Reputation: 177

If you are using javascript, you can use this fragment

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

Upvotes: 0

Tan Li Hau
Tan Li Hau

Reputation: 2334

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.

Upvotes: 1

Related Questions