markhamknight
markhamknight

Reputation: 375

Regex Pattern for checking the first letter of each word in a string if its Uppercase in Javascript

for example my string is Foo Bar. this string should match the pattern.

if the string is Foo bar. the string should not match.

if the string is Foo Bar Foobar the string should match

if the string is Foo. it should also match.

so far I only have this pattern

 (^[A-Z]{1}.*(\s)?$)+

Basically I will only accept a string where each First letter of each word is Uppercase

Upvotes: 8

Views: 28734

Answers (5)

3gwebtrain
3gwebtrain

Reputation: 15303

Can you check this?

/^([A-Z]\w*\s*)+$/

Live Demo

Upvotes: 0

ינון רחמים
ינון רחמים

Reputation: 706

I used this:

/(^[a-z]| [a-z])/ig

Upvotes: 0

Mohammad Awni Ali
Mohammad Awni Ali

Reputation: 115

I used to use this:

#(\s|^)([a-z0-9-_]+)#i

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172608

You can try to use this regex:

^(\b[A-Z]\w*\s*)+$

Regex Demo

Upvotes: 5

ahaurat
ahaurat

Reputation: 4255

I'd see if your string does NOT match something like this:

/\b[a-z]/

Upvotes: 10

Related Questions