0

I'm making an API called Server.js Now, what I'm trying to do is change the HTML text ({date}) turn to the actual date. Instead of changing it to the actual date it stayed as ({date}). I'm wondering how I can fix this. Can you help me? This is my code:

var doc = document.toString();
var doc1 = doc.replace("\(\{date\}\)", new Date());
doc.innerHTML = "";
doc.innerHTML = doc1;

Any help appreciated!

1 Answers1

1

I don't think you need the escaped characters. Just make it a normal string.

var doc1 = doc.replace("({date})", new Date());

EDIT: There are several ways you could possibly replace all occurrences. A simple search would reveal questions like How to replace all occurrences of a string in JavaScript?

You can try a regular expression:

var doc1 = doc.replace(new RegExp("\\({date}\\)", 'g'), new Date());
Kyle Delaney
  • 11,616
  • 6
  • 39
  • 66