Cumbersome time parsing in JavaScript
<p>I need a function to convert time in text from a format with day-part letters to digits.
E.g. 4:15PM -> 16:15, 4:15AM -> 4:15AM. Currently I have the following solution</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>function formatTime(text){
 var find = '([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9] (AM|PM)';
 
 var reg = new RegExp(find, 'g');
 
 pos = 0;

 var result;
 var formatedText = "";
 while((result = reg.exec(text)) !== null) {
  if(result[2] == "PM"){
   var hours= parseInt(result[0], 10);
   hours = hours + 12;
   var hoursStr = hours.toString();
   var newTime = hoursStr + result[0].substring(result[1].length,result[0].length - 3);
   
   formatedText += newTime;
   pos = reg.lastIndex;
  } else {
   formatedText += text.replace("AM","").substring(pos, reg.lastIndex);
   pos = reg.lastIndex;
  }
 }
 
 if(pos &lt; text.length){
  formatedText += text.substring(pos, text.length);
 }
 
 return formatedText;
}

console.log(formatTime("Some Text (11:00AM - 1:00PM)"));</code></pre>
</div>
</div>
</p>

<p>I makes nicely cases like 
console.log(formatTime("Some Text (11:00AM - 1:00PM)"));</p>

<p>But I strugle to make it process
console.log(formatTime("Some Text (11:00 AM - 1:00 PM)"));</p>