I'm trying to write a regular expression to remove white spaces from just the beginning of the word, not after, and only a single space after the word.
This is the regex that I've tried:
var re = new RegExp(/^([a-zA-Z0-9]+\s?)*$/);
Output -
re.test("")
true - required false
re.test(" ")
false
re.test(" word")
false - required word - true
re.test(" word ")
false - required word - true
re.test("word ")
true
re.test("word ")
false - - required word(with single space ) - true
Exp:
See, I have to send a request to backend where if
1) key term is ""(blank) no need to send the request.
2) If key is " string" need to send the request.
3) If user enter something like this "string string" need to send the request.
4) if user enter "string " need to send the request.
5) if user enter string with a space need to send the request but at the moment he enter another space no need to send the request.
This thing i am trying to achieve using regular expression.