Replace words dynamically with JavaScript
<p>With this line of code</p>

<pre><code>$("body").html($("body").html().replace(/1800/g,'6:00pm'));
</code></pre>

<p>I can replace the word, but how can I make it more dynamic with this script?</p>

<pre><code>var getFormattedTime = function (fourDigitTime){
    var hours24 = parseInt(fourDigitTime.substring(0,2));
    var hours = ((hours24 + 11) % 12) + 1;
    var amPm = hours24 &gt; 11 ? 'pm' : 'am';
    var minutes = fourDigitTime.substring(2);

    return hours + ':' + minutes + amPm;
};
</code></pre>

<p>My HTML code is e.g</p>

<pre><code>Your class will start at 1900, please be there by 1845.
</code></pre>

<p>Which will be counted to</p>

<pre><code>Your class will start at 07:00pm, please be there by 06:45pm.
</code></pre>

<p>I tried this:</p>

<pre><code>$("body").html($("body").html().replace(fourDigitTime, getFormattedTime(fourDigitTime)));

var fourDigitTime = document.getElementById('fourDigitTime').value;

var getFormattedTime = function (fourDigitTime){
var hours24 = parseInt(fourDigitTime.substring(0,2));
var hours = ((hours24 + 11) % 12) + 1;
var amPm = hours24 &gt; 11 ? 'pm' : 'am';
var minutes = fourDigitTime.substring(2);

return hours + ':' + minutes + amPm;
};
</code></pre>

<p>It does not work..</p>
