Varathan Swaminath Varathan Swaminath - 1 year ago 46
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;

Answer Source

If you want to remove whitespace just from the beginning

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