title: Regex Help in some Javascript code
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5938115/regex-javascript-to-transform-degrees-decimal-minutes-to-decimal-degrees">Regex/Javascript to transform Degrees Decimal Minutes to Decimal Degrees</a>  </p>
  </blockquote>
  
  
  
  <p>I have some javascript code that converts Decimal Degree Minutes to Decimal Degrees. But something is slightly screwy and am hoping someone here will know how to fix it. </p>
  
  <p><strong>How to I get this to consistently work when a user removes the <code>°</code> or has extra spaces?</strong></p>
  
  <p>With the <code>°</code> character it works great:</p>
  
  <pre><code>input "77° 50.51086497"
  output "77.8418477495" 
  
  input "-113° 40.54687527"
  output "-113.6757812545"
  </code></pre>
  
  <p>However without the <code>°</code> it sometimes breaks:</p>
  
  <p><strong>NOT WORKING</strong></p>
  
  <pre><code>input "77 50.51086497"
  ouput "775.0085144161667" 
  </code></pre>
  
  <p><strong>WORKING</strong></p>
  
  <pre><code>input "-113 40.54687527"
  output "-113.6757812545" 
  </code></pre>
  
  <p>Same thing with extra spaces:</p>
  
  <p><strong>NOT WORKING</strong></p>
  
  <pre><code>input "77   ` ` ` ` ` ` ` `50.51086497" 
  output "775.0085144161667" 
  </code></pre>
  
  <p><strong>WORKING</strong></p>
  
  <pre><code>input "-113   ` ` ` ` ` ` ` `40.54687527 "
  output "-113.6757812545" 
  </code></pre>
  
  <p>Here is my code: <a href="http://jsfiddle.net/NJDp4/4/" rel="nofollow noreferrer"><strong>Please see the JSFIDDLE TO TEST</strong></a></p>
  
  <pre><code>function ddmToDeg(ddm) { 
      if (!ddm) { 
          return Number.NaN; 
      } 
      var neg= ddm.match(/(^\s?-)|(\s?[SW]\s?$)/)!=null? -1.0 : 1.0; 
      ddm= ddm.replace(/(^\s?-)|(\s?[NSEW]\s?)$/,''); 
      ddm= ddm.replace(/\s/g,''); 
      var parts=ddm.match(/(\d{1,3})[.,°d]?(\d{0,2}(?:\.\d+)?)[']?/); 
          //dms.match(/(\d{1,3})[.,°d]?(\d{0,2})[']?(\d{0,2})[.,]?(\d{0,})(?:["]|[']{2})?/);
      if (parts==null) { 
          return Number.NaN; 
      } 
      // parts: 
      // 0 : degree 
      // 1 : degree 
      // 2 : minutes 
  
  
      var d= (parts[1]?         parts[1]  : '0.0')*1.0; 
      var m= (parts[2]?         parts[2]  : '0.0')*1.0; 
      var dec= (d + (m/60.0))*neg; 
      return dec; 
  }
  </code></pre>