Javascript: String validation using regex
Problem: Validate a string if its in Sentence case
Ex-1: Account
output: true
Ex-2: information
output: false
First create a regex to validate the string;
const regex = new RegExp('^[A-Z][a-z]*');
^
represents the start of the string
[A-Z]
for validating the character should be in A to Z
similarly followed by smaller cases.
const str1 = 'Account';
const str2 = 'information';
const regex = new RegExp('^[A-Z][a-z]*');
console.log(regex.test(str1)); // outputs to true
console.log(regex.test(str2)); // outputs to false