简体   繁体   中英

Regex validation space in the middle of a string

I'm looking for an expression that requires a space in a string, it doesn't have to be dead in the middle just not at the end (or start).

I've had a look on google and stack-overflow, there are quite a few but I haven't found one that does what I need.

Here's what I have at the moment

 var re = /^[A-Z]\'?[- a-zA-Z]( [a-zA-Z])*$/igm;

Based on the limited requirements you specified, this will do it. It requires a string to contain ONE space, anywhere but at the start or end.

/^[^ ]+ [^ ]+$/

Explanation: anchoring to the beginning of the string, allow one or more non-space characters, followed by a single space, followed by, again, one or more non-space characters, to the end of the string.

[^ ] is a negated character class. That is, it says "anything but the characters inside [ and ] .

Your regex should be: /^[AZ]\\'?[-\\sa-zA-Z](\\s[a-zA-Z])*$/igm; . According to my idea, regex doesn't recognize a whitespace that why I replace those with \\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