Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 678
markhamknight Avatar asked May 10 '16 06:05

markhamknight


People also ask

How do you check if the first letter of a string is uppercase JavaScript?

To check if the first letter of a string is uppercase, call the toUppercase() method to convert the first letter to uppercase and compare it to itself. If the comparison returns true , then the first letter is uppercase.

How do I get the first letter of a capital in regex?

Example 2: Convert First letter to UpperCase using Regex The regex pattern is /^./ matches the first character of a string. The toUpperCase() method converts the string to uppercase.

How do you match a capital letter in regex?

Using character sets For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter. In a character set a ^ character negates the following characters.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.


2 Answers

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

/\b[a-z]/
like image 157
ahaurat Avatar answered Oct 02 '22 16:10

ahaurat


You can try to use this regex:

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

Regex Demo

like image 41
Rahul Tripathi Avatar answered Oct 02 '22 15:10

Rahul Tripathi