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

0

2 Answers 2

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 -> \\`);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this, weird it's not a function - I'll go with this
@BenTaliadoros You can look at the template strings reference for more info about backticks developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
2

put \\\\ every \\ represents one \

this.sanitizer.bypassSecurityTrustHtml("Double backslashes are being removed -> \\\\")

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.