Skip to content Skip to sidebar Skip to footer

Regex Help In Some Javascript Code

Possible Duplicate: Regex/Javascript to transform Degrees Decimal Minutes to Decimal Degrees I have some javascript code that converts Decimal Degree Minutes to Decimal Degrees.

Solution 1:

I answered this on the original question: Regex/Javascript to transform Degrees Decimal Minutes to Decimal Degrees

Solution posted to your fiddle: http://jsfiddle.net/NJDp4/6/

dmsToDeg: function(dms) { 
        if (!dms) { 
            returnNumber.NaN; 
        } 
        var neg= dms.match(/(^\s?-)|(\s?[SW]\s?$)/)!=null? -1.0 : 1.0; 
        dms= dms.replace(/(^\s?-)|(\s?[NSEW]\s?)$/,''); 
    var parts=dms.match(/(\d{1,3})[.,°d ]?\s*(\d{0,2}(?:\.\d+)?)[']?/); 
        if (parts==null) { 
            returnNumber.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; 
    }

The reason this was working for "-113 40.54687527" but not for "77 50.51086497" is that the code was ripping out spaces and the sign (leaving "11340.54687527" and "7750.51086497") and then grabbing the first 3 digits to use as degrees (so "113" and "775"). I've modified this so that it no longer strips out spaces before grabbing the two numbers.

Post a Comment for "Regex Help In Some Javascript Code"