我正在尝试创建一个正则表达式,用于匹配落在正确范围内的纬度/经度坐标。但是下面的代码给出了错误。请告诉我出错的地方:
Angular:
var app = angular.module('myapp', []);
app.controller('latlong', function($scope) {
$scope.name = "Valiadtion";
$scope.lat = "latitude";
$scope.long = "longitude";
$scope.submit = function() {
var lat = $scope.lati;
var reg = /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/;
if (reg != lat) {
console.log('error');
}
}
});
Here是Plunker
答案 0 :(得分:1)
而不是==
,您必须使用此
var reg = /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/;
var lat = "+90.0, -127.554334";
if (!lat.match(reg)) {
console.log('error');
} else {
console.log('passed!');
}

详细了解https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match