我使用AngularJS指令进行我的表单输入验证。有没有办法让我的正则表达式只允许一个特殊字符(:)以及数字和字母。这是我在stackoverflow上找到的指令。正则表达式允许一个特殊字符(:)和数字和字母只
myapp.directive('noSpecialChar', function() {
return {
require: 'ngModel',
restrict: 'A',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function(inputValue) {
if (inputValue == null)
return ''
cleanInputValue = inputValue.replace(/[^\w\s]/gi, '');
cleanInputValue = cleanInputValue.toUpperCase();
if (cleanInputValue != inputValue) {
modelCtrl.$setViewValue(cleanInputValue);
modelCtrl.$render();
}
return cleanInputValue;
});
}
}
});
我试着在最后添加冒号,但我无法达到我的解决方案。
尝试'.replace(/ ^([\:\] \ :) | [^ \ w \ s]/g,function($ 0,$ 1){return $ 1?$ 1.replace(/ [^ \ w \ s ] +/g,'')+':':'';})''而不是'.replace(/ [^ \ w \ s]/gi,'') –