0

I need to render a string on the page with double backslash "\\" but Angular removes one from the temaplte, treating it like a regular expression.

An example of the issue is here: https://codepen.io/bental/pen/xxZdYZv

I have also tried escaping with and without DomSanitizer but can't seem to make it work.

I don't want to manipulate the data as it's coming from the backend and can't account for every possibility

Ben Taliadoros
  • 7,003
  • 15
  • 60
  • 97

2 Answers2

3

If you don't need IE/Opera support, you can use String.raw (note that you need to use backticks instead of double quotes for it to work as intended)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw

this.sanitizer.bypassSecurityTrustHtml(String.raw`Double backslashes are being removed -> \\`);
Alberto Rivera
  • 3,652
  • 3
  • 19
  • 33
2

put \\\\ every \\ represents one \

this.sanitizer.bypassSecurityTrustHtml("Double backslashes are being removed -> \\\\")
mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54
  • I get the data from the backend - I'll update the post to make this clear. I don't really want to manipulate the string for each instance of this - is it just backslashes? – Ben Taliadoros Jun 23 '20 at 16:06