6

我正在玩 Angular 并编写一个正则表达式测试器。问题是当我输入数据时,前导空格被修剪。在此处查看示例jsfiddle

因此,当页面加载时,我有 RegEx "^\d+$".test(" 123 ") 导致“不匹配”,但是如果您在候选框中输入额外的前导或尾随空格:

  1. 从我的变量中删除了前导和尾随空格
  2. 结果更改“匹配”

这是我的 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', []);
4

1 回答 1

12

更新了小提琴,在输入标签中添加了 ng-trim="false"

http://jsfiddle.net/T2zuV/12/

<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" ng-trim="false"/>Candidate:
            <input type="text" ng-model="candidate" ng-trim="false"/>
            <br />.{{candidate}}.
            <br>.{{candidate2}}.</form>
    </div>
</div>
于 2013-07-19T07:41:36.293 回答