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
  • 26,856
  • 59
  • 237
  • 450
  • 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
  • 761,203
  • 64
  • 569
  • 643