我很想檢測一個字符串與正則表達式匹配的位置,但這似乎失敗了模糊。請參閱此plunkr。angularjs:正則表達式失敗onblur
這裏的JS:
var endsWithPipeOrSpace = /\s+$|\|$/g;
$scope.inputGetFocus = function() {
$scope.msg = "input gained focus"
if (endsWithPipeOrSpace.test($scope.newquery)) {
$scope.msg += " string match";
} else {
$scope.msg += " string doesn't match";
}
}
$scope.inputLostFocus = function() {
$scope.msg = "input has lost focus"
if (endsWithPipeOrSpace.test($scope.newquery)) {
$scope.msg += " string match";
} else {
$scope.msg += " string doesn't match";
}
}
$scope.newQueryModified = function() {
$scope.msg = "input modified"
if (endsWithPipeOrSpace.test($scope.newquery)) {
$scope.msg += " string match";
} else {
$scope.msg += " string doesn't match";
}
}
和這裏的HTML:
請注意我聲明瞭NG-裝飾= 「假」
<input type="text" class="form-control"
id="newquery" ng-model="newquery"
placeholder="Enter your query"
autofocus required autocomplete="off"
ng-change="newQueryModified()"
ng-trim="false"
ng-focus="inputGetFocus()"
ng-blur="inputLostFocus()">
非常感謝,良好和簡潔的解釋! – datamosh 2014-09-30 09:22:02