0

I have this regex /^[A-Za-z.,' -]+$/

But unfortunately, /^[A-Za-z.,' -]+$/.test(' ') returns true.

How can I ensure that there is at least 1 non space character.

dagda1
  • 23,659
  • 54
  • 216
  • 399
  • similar question https://stackoverflow.com/questions/5599934/regular-expression-that-allows-spaces-in-a-string-but-not-only-blank-spaces – Shrugo Oct 26 '17 at 19:47

1 Answers1

2

You can use a lookahead assertion in your regex:

/^(?=\s*\S)[A-Za-z.,' -]+$/

(?=\s*\S) is a positive lookahead to assert we have a non-space character ahead after matching 0 or more spaces.

anubhava
  • 713,503
  • 59
  • 514
  • 593