gpt4 book ai didi

html - 使用输入标签时如何保留前导和尾随空格?

转载 作者:太空狗 更新时间:2023-10-29 14:47:28 25 4
gpt4 key购买 nike

我正在玩 Angular 并编写一个 Regex 测试器。问题是当我输入数据时前导空格被修剪。参见示例 jsfiddle here :

因此,当页面加载时,我有 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', []);

最佳答案

更新了 fiddle ,在输入标签中添加了 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>

关于html - 使用输入标签时如何保留前导和尾随空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17739726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com