简体   繁体   中英

Regular expression: at least one blank space enclosed by any character

I'd like to force my users to have at least two words in a string divided by a blank space.

Allowed is any string that has at least one blank space enclosed by at least one character to the left and to the right. No blank space on the front and no blank space at the end.

Some examples:

  • John Doe (valid)
  • John Walter Doe (valid)
  • John (not valid)
  • John $ole (valid)
  • 3d3n 1snt f4r (valid)
  • John the 3rd (valid)
  • Jöning Wölter (valid)
  • Joe Garçon (valid)
  • Harald Spaß (valid)

Just FYI: I want to use this for a very weak full name check. Since there are MANY special characters that can occur in names, I simply want my users to force to have at least two words in a string.

If I understand your requirement, it's translated like this in regex:

^\S+\s+.*\S$

If you want your white space to be just " ", then use

^\S+ +.*\S$

But be careful that you make cultural assumptions here. Real names don't follow strict rules.

This matches a word followed by one or more groups of whitespace + word

/^\S+(\s+\S+)+$/

It does match any whitespace though so perhaps

/^\S+( +\S+)+$/

would be better

尝试这样的事情:

^(\S+)(\s{1}\S+)+$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM