-1

I want to render a string in HTML with all it's escape sequence intact:

Like This:

eipt No. : 1995,\nmount 935\nAdm. : 17/06/2016\n\nINSTRUCTIONS :\n\n4) This cards is valid only for\nstudent name & for the period\nindicated.\n\n2) This Card is not transferable and\nmust be produced whenever\ndemandes\n\n3) In case of its loss, the holder of\nthis card must intimate to the\nPrincipal immediately in writing.\n\n4) \fanybody finds this card, he/she\nis requested to reach the\nPrincipal.\n\n \n\x0c

I am receiving strings like this from ajax request and I am setting the value of some tag like this:

$('#ExtractedText').text(responce["text"]);

Everything is working but string is not in its raw form. It looks something like this:

enter image description here

I want it to look in it's raw format. How can I achieve this?

Andreas
  • 21,535
  • 7
  • 47
  • 56
luckyCasualGuy
  • 641
  • 1
  • 5
  • 15
  • 1
    Escape the escape sequences – Andreas Oct 17 '20 at 13:46
  • @Andreas That means I have to process the string at backend. Which makes my web app slow ! if you can provide me with a way to do it using some HTML or JS will be much appreciated !! – luckyCasualGuy Oct 17 '20 at 13:50
  • _"That means I have to process the string at backend. Which makes my web app slow !"_ - If a simple string replacement _"makes your web app slow"_ then you have bigger problems than these escape sequences – Andreas Oct 17 '20 at 13:53
  • The answer is in my first comment. Just do some research. It's not that complicated. – Andreas Oct 17 '20 at 13:56
  • Dude @Andreas is right XD – Zinho Oct 17 '20 at 13:57
  • You don't know what I am dealing with here so it only makes sense to answer the question accordingly. – luckyCasualGuy Oct 17 '20 at 13:58

1 Answers1

0

Use a PRE to format

const str = `Receipt No. : 1995,\nmount 935\nAdm. : 17/06/2016\n\nINSTRUCTIONS :\n\n4) This cards is valid only for\nstudent name & for the period\nindicated.\n\n2) This Card is not transferable and\nmust be produced whenever\ndemandes\n\n3) In case of its loss, the holder of\nthis card must intimate to the\nPrincipal immediately in writing.\n\n4) \fanybody finds this card, he/she\nis requested to reach the\nPrincipal.\n\n \n\x0c`
document.querySelector("pre").innerText=str;
<pre></pre>

Use String.raw to not - see How to escape backslash in JavaScript?

const str = String.raw`Receipt No. : 1995,\nmount 935\nAdm. : 17/06/2016\n\nINSTRUCTIONS :\n\n4) This cards is valid only for\nstudent name & for the period\nindicated.\n\n2) This Card is not transferable and\nmust be produced whenever\ndemandes\n\n3) In case of its loss, the holder of\nthis card must intimate to the\nPrincipal immediately in writing.\n\n4) \fanybody finds this card, he/she\nis requested to reach the\nPrincipal.\n\n \n\x0c`

document.getElementById("div1").innerText = str;
<div id="div1"></div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236