0

User input is saved in $scope.userInput. I want to check if the user has entered anything in the input, but I want to ignore whitespace and HTML tags, and check only for other kinds of input.

How can I do that?

4
  • s/escape/ignore/…? You want to ignore whitespace and HTML tags…? Commented Nov 16, 2016 at 9:01
  • yes, ignore, I didn't know the correct vocabulary. I will edit the question. Commented Nov 16, 2016 at 9:01
  • Do you mean you don't want to allow a user to submit by just pressing space a few times? Commented Nov 16, 2016 at 9:18
  • 1
    do you want to validate it? You can simply check for blank using length property of userInput.length > 0 // has entered something. Ignore html and whitespace String(text).replace(/<[^>]+>/gm, '') Commented Nov 16, 2016 at 9:20

2 Answers 2

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 

Explanation of the regular expression (<([^>]+)>) :

---------------------------------------------------------------------- ( 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 ---------------------------------------------------------------------- 

Working Demo :

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>

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.