我正在玩Angular并编写一个Regex测试程序。 问题是我输入数据时导致空白被裁剪。请参阅示例jsfiddle here:如何在使用输入标签时保留前导空白和尾随空白?
因此,当页面加载时,我有RegEx“^ \ d + $”。test(“123”)导致“不匹配”,但是如果在输入一个额外的前导或尾部空格候选框:
- 开头和结尾的空格从我的变量
- 结果中删除改变 “匹配”
这里是我的HTML:
<div id='ng:app' class='ng-app: myApp' ng-app='myApp'>
<div ng-controller="Controller">{{addTodo2()}}
<form novalidate class="simple-form">Pattern:
<input type="text" ng-model="pattern" />Candidate:
<input type="text" ng-model="candidate" />
<br />.{{candidate}}.
<br>.{{candidate2}}.</form>
</div>
</div>
这里是相关联的JavaScript:
function Controller($scope) {
$scope.pattern = "^\\d+$";
$scope.candidate = " 123 ";
$scope.candidate2 = " 123 ";
$scope.addTodo2 = function() {
var str = "Javascript is an interesting scripting language";
var re = new RegExp($scope.pattern, "g");
var result = re.test($scope.candidate);
if (result) {
return "Match22";
} else {
return "No Match22";
};
};
}
var myapp = angular.module('myApp', []);
你见过或尝试纳克-trim =“false”在你的输入中? – shaunhusain