简体   繁体   中英

Only allow a-z and space and the first letter of each word to be capitalized

Someone sitting on a regex that only allows az and ONLY allow the first letter of each word to be capitalized?

So 'Im detective John kimble" would be match but "Im a Cop yOu iDiot" would not be allowed

This regex will match a word with a lower-case or capital letter at the beginning of the word.

[a-zA-Z][a-z]*

Now you can extend the regex to match multiple such words depending on what exactly you want. You have to be a bit careful with this to make sure it handles strange cases like an empty sentence etc.

([a-zA-Z][a-z]*)* // Matches the empty sentence as well
([a-zA-Z][a-z]*)+ // Must have at least one word

Then you need to consider if the start and end characters ( ^ and $ ) are relevant for your pattern.

In css you can capitalize the 1st letter of each word with:

.title {
    text-transform: capitalize;
}

In PHP the string function ucfirst like this:

$foo = ucfirst($foo);

Allows only az use this regex in Javascript

var pat = /^[a-z]+$/;

You really don't need regex for this that .. because i don't really think how is is an offence

You can simple correct the case :

$str = "joHn KiMBle";
echo ucwords(strtolower($str));  // John Kimble 

Try using

([a-zA-Z][a-z]*)+

Hope it helps

您可以使用此模式检查以下内容:

^(?>[A-Za-z][a-z]*+|[^A-Za-z]++)+$

Doable without regex.

!(strspcn($text, "0123456789") !== false || 
  ucwords($text) == ucwords(strtolower($text)))

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