Roma M'
Roma M'

Reputation: 23

Regex condition for extra white spaces between words in a string

^(?=\S)^[ `\'\.\-&A-Za-z0-9u00C0-u017F]+(?<=\S)$

Is what I've come up with but I don't know how to check for white spaces extra white spaces between words in the middle of the string

Upvotes: 2

Views: 77

Answers (1)

PJProudhon
PJProudhon

Reputation: 825

What you don't want: leading or trailing white spaces or extra white spaces between words. Then you want a string composed of single white space delimited words.

I'll proceed with ^\S+(?:\s\S+)*$.

In details:

^      # Matches at the beginning of the string
\S+    # Matches one or more non-spacing character
(?:    # Starts a non-capturing group
  \s   # Matches one spacing character
  \S+  # Matches one or more non-spacing character
)*     # Repeat non-capturing group zero or more times
$      # Matches at the end of string

Testing here

Upvotes: 1

Related Questions