在字符串angularjs中刮取特殊符号

时间:2016-10-27 07:59:24

标签: angularjs character symbols

我有一个包含连字符" - "的字符串。如果我想要这些字母,如何废除连字符?例如,我有这个String" -KTxEMxrAY"。如何删除连字符,使其成为" KTxEMXAY" ?

我正在使用AngularJS。

3 个答案:

答案 0 :(得分:2)

你可以这样做,你想要的确切:

    function ClickToEditCtrl($scope) {
    $scope.modelFilter = '-KTxEMxrAY';
    $scope.modelFilter = $scope.modelFilter.replace('-','');
    
    }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js"></script>
<div ng-app>
  <div ng-controller="ClickToEditCtrl">
   {{ modelFilter}}
  </div>
</div>

答案 1 :(得分:1)

使用正则表达式,如

var string = string.replace(/[^a-z]/gi, '');

答案 2 :(得分:1)

否则你可以使用过滤器:

&#13;
&#13;
function ClickToEditCtrl($scope) {
$scope.modelFilter = '-KTxEMxrAY';
//$scope.modelFilter = $scope.modelFilter.replace('-','');
 $scope.modelFilterNormalized = function(){
     return $scope.modelFilter = $scope.modelFilter.replace('-','');
};
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app>
  <div ng-controller="ClickToEditCtrl">
   {{ modelFilter | filter:modelFilterNormalized() }}
  </div>
</div>
&#13;
&#13;
&#13;