2016-11-16 20 views
0

ユーザー入力は$scope.userInputに保存されます。ユーザーが入力に何かを入力したかどうかを確認したいが、空白とHTMLタグを無視し、他の種類の入力のみをチェックしたい。エスケープHTMLと空白?

どうすればいいですか?

+0

'S /エスケープ/無視/' ...? *空白とHTMLタグを無視したいのですが...? – deceze

+0

はい、無視して、私は正しい語彙を知らなかった。私は質問を編集します。 – user1283776

+0

スペースを数回押すだけでユーザーが送信を許可したくないということですか? – Crowes

答えて

1
// 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>

1

var regex = /(<([^>]+)>)|\s/ig; 
 
var string = "Test white space and <p>HTML tags</p>"; 
 
var result = string.replace(regex, ""); 
 
console.log(result); // TestwhitespaceandHTMLtags