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.
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.
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.