I have a javascript string like below:
var content = 'Robert John Testy'
content = content.replace('John','<MiddleName>');
It returns 'Robert Testy' instead of 'Robert <MiddleName> Testy'
Any idea's? Thanks.
I have a javascript string like below:
var content = 'Robert John Testy'
content = content.replace('John','<MiddleName>');
It returns 'Robert Testy' instead of 'Robert <MiddleName> Testy'
Any idea's? Thanks.
Looks like you're running it in HTML context:
In that case you must use '<MiddleName> which will be displayed as <MiddleName> when rendered in HTML
var content = 'Robert John Testy'
content = content.replace('John','<MiddleName>');
Just demonstrating the difference between both:
 <html>
  <body>
     <div id="results1"> </div>
     <div id="results"> </div>
     <script type="text/javascript">
             var content = 'Robert John Testy'
             content = content.replace('John','<MiddleName>');
             document.querySelector("#results").innerHTML =content;
             content = 'Robert John Testy'
             content = content.replace('John', '<MiddleName>');
                
             document.querySelector("#results1").innerHTML =content;
     </script>
  </body>
 </html>
 