Varathan Swaminath Varathan Swaminath - 3 months ago 7x
Javascript Question

I need a regular expression to remove white space in the beginning and it should allow spaces in between two words

Here is my source code. I have written a directive in angularjs to eliminate spaces. It's perfectly working for spaces between words but is still allowing spaces at the beginning.

function customValidation() {

return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {

modelCtrl.$parsers.push(function (inputValue) {

var transformedInput = inputValue.toLowerCase().replace(/ /g,'');

if (transformedInput!== inputValue) {

return transformedInput;


If you want to remove whitespace just from the beginning

inputValue.toLowerCase().replace(/^\s+/, '').replace(/\s+/g, ' ');