This is my js code that deletes multiple spaces in string and trim string:
$scope.fixInput = function (input){
input = input.replace(/\s+/g, ' ').trim();
return input;
};
And i have input field in html:
<input type="text" ng-model="userName" ng-change="userName=fixInput(userName)"/>
And there is a strange behaviour. When i type for example
" a a a "
the result will be fine
"a a a"
but when i type
" aaa "
when it has to just to trim string the result is not changing, it's the same.
var v= " aaa "; v.replace(/\s+/g, ' ').trim();and found the output asaaa. So is it fine or you are expecting something else?var app = angular.module("test",[]) .controller("TestController", function($http,$scope){ var vm = this; vm.my = true; $scope.fixInput = function (input){ console.log(input); input = input.replace(/\s+/g, ' ').trim(); return input; }; });