How do preserve leading and trailing whitespace when using an input tag?
angularjs, html
Solution
Updated the fiddle, added ng-trim="false" to the input tags
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>
Problem
I am playing with Angular and writing a Regex tester. Problem is leading whitespace is trimmed when I enter data. See example jsfiddle here: So when the page loads I have the RegEx "^\d+$".test(" 123 ") which results in "No Match", But if you enter an extra leading or trailing space in the Candidate box: - The leading and trailing spaces are removed from my variable - The result changes "Match" Here is my 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> ``` And here is the associated 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', []); ```