0

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.

Tim
  • 41,901
  • 18
  • 127
  • 145
Soarabh
  • 2,910
  • 9
  • 39
  • 57
  • Elaborate the task more please. "to remove white spaces from beginning" while in your examples you don't use `replace()` but a `test()` – zerkms May 24 '13 at 14:11
  • Are you trying to only remove one whitespace char after the word, and leave any other whitespace chars there, or are you trying to reduce more than one whitespace character at the end to be no more than one space? Or are you trying to assert that the word must simply have a single space at the end to match? – ajp15243 May 24 '13 at 14:17

4 Answers4

2

I think following should take care of all the space replacements for you:

var str      = "   this is     my  string    ";
var replaced = str.replace(/^\s+|\s+$|\s+(?=\s)/g, "");
// repl = 'this is my string'
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Another possibilities for above: `var replaced = str.replace(/\s{2,}/g, ' ').trim();` which replaces 2 or more white spaces with a single space, and trims the start/end white spaces. – erosman Apr 30 '14 at 07:22
  • Yes that will also work but requires 2 method calls. – anubhava Apr 30 '14 at 07:24
1

This would fit your case:

var myString = "   this is my string    ";
myString = myString.replace(/^\s+/ig, "").replace(/\s+$/ig,"")+" ";
alert(myString);

Tinker: http://tinker.io/e488a/1

Sébastien Renauld
  • 19,203
  • 2
  • 46
  • 66
0
// this should do what you want
var re = new RegExp(/^([a-zA-Z0-9]+\s?)+$/);
bert
  • 466
  • 3
  • 7
  • I just had a look at the tests... I don't know why you want the input "" to fail. – bert May 24 '13 at 14:20
  • Have you changed your mind? Look at your question - You want "saorabh " (with 2 blanks) to fail. – bert May 24 '13 at 20:20
0

I think the word boundary perfectly suits for this. Try this regexp:

var myString = "   test    ";
myString = myString.replace(/\s+\b|\b\s/ig, "");
alert('"' + myString + '"');

The regexp removes all spaces before all words and the first space after all words

Lukas Kral
  • 987
  • 13
  • 22