0

I am trying to escape interpolation markers {{ and }} in the input string to avoid them being executed as interpolation by angularjs 1.8.3. These markers should be escaped by preceding each of their characters with a REVERSE SOLIDUS U+005C (backslash).

this is what I tried:

var inputString = "{{ code snippet here }}" ; 
inputString = inputString.replace("{{", "\\{\\{").replace("}}","\\}\\}");

WHen i set this string back to Html, it gets rendered in output Html as:

\{\{ code snippet here \}\}

I'm expected to show exactly the same as they were in inputString (without \)
How do I escape these markers without affecting them in output Html?

P.S. I can't use ngNonBindable which was mentioned in this answer because I can't modify all the Html at this point. And also due to other project-specific reasons.

Code Snippet:

var nodeValue = "{{ 'sample snippet' }}";
nodeValue = nodeValue.replace('{{', '\\{\\{').replace('}}','\\}\\}');

document.getElementById('testtextarea').innerText = nodeValue;
<div ng-app>

<textarea id="testtextarea" cols="50"></textarea>


<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.8.3/angular.min.js"></script></div>
ScrapCode
  • 2,109
  • 5
  • 24
  • 44
  • I might be missing something... Why u need to escape value? https://plnkr.co/edit/jbsUVnjspm8EupQ1 or do u set it before angular compiles element? – Petr Averyanov Sep 21 '22 at 03:49

1 Answers1

0

Try replacing the characters with their relative HTML entities:

inputString = inputString.replace("{{", "&lcub;&lcub;").replace("}}","&rcub;&rcub;");