ユーザー入力は$scope.userInput
に保存されます。ユーザーが入力に何かを入力したかどうかを確認したいが、空白とHTMLタグを無視し、他の種類の入力のみをチェックしたい。エスケープHTMLと空白?
どうすればいいですか?
ユーザー入力は$scope.userInput
に保存されます。ユーザーが入力に何かを入力したかどうかを確認したいが、空白とHTMLタグを無視し、他の種類の入力のみをチェックしたい。エスケープHTMLと空白?
どうすればいいですか?
// Input string
$scope.userInput = "<h1>My input</h1>";
// Removed HTML tags from the string.
var removedTagStr = $scope.userInput.replace(/(<([^>]+)>)/ig,'');
// Removed the white spaces from the string.
var result = removedTagStr.replace(/ /g,'');
console.log(result); // Myinput
正規表現
(<([^>]+)>)
の
説明:
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
< '<'
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
[^>]+ any character except: '>' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
> '>'
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
ワーキングデモ:
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl',function($scope) {
$scope.userInput = "<h1>My input</h1>";
var removedTagStr = $scope.userInput.replace(/(<([^>]+)>)/ig,'');
$scope.result = removedTagStr.replace(/ /g,'');
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
{{result}}
</div>
var regex = /(<([^>]+)>)|\s/ig;
var string = "Test white space and <p>HTML tags</p>";
var result = string.replace(regex, "");
console.log(result); // TestwhitespaceandHTMLtags
'S /エスケープ/無視/' ...? *空白とHTMLタグを無視したいのですが...? – deceze
はい、無視して、私は正しい語彙を知らなかった。私は質問を編集します。 – user1283776
スペースを数回押すだけでユーザーが送信を許可したくないということですか? – Crowes