I'm just starting with regular expressions and I don't know how to solve this one. It's about making a pattern in JavaScript that starts with a capital letter and ends with a period. This is the code I have done so far:
function validarFrase() {
var frase = document.getElementById("frase").value;
var reg=new RegExp("^[A-Z]+[.]$");
if (frase.match(reg)){
alert ("El formato es el correcto");
} else {
alert ("El formato es incorrecto");
}
}
Escriba su frase<input type="text" id ="frase"/>
<button onclick="validarFrase()">Verificar</button>
The case is that I enter a name, for example, "Paco." that meets the requirements of the regular expression and it doesn't match me, because the regular expression is wrong, but I don't know how to make it right. Some help? Thanks.
If it must start with a capital letter and end with a period and everything between them can be lowercase or uppercase or a number, you can use this regular expression.
^[A-Z].*[.]$
For example:
If you want only letters to go, case sensitive, you can use the following regular expression.
^[A-Z][A-z]+[.]$
Where guys from this class will match you.
I hope I was able to help you.
Try this expression:
It indicates the following:
^[A-Z]
: start with 1 uppercase letter[a-z]+
: containing 1 or more lowercase letters.[.]$
: that at the end of the text has the period character (.
).Examples:
In case you want to know why your expression fails, it is because it expects the beginning of the string to have 1 or more uppercase letters and then to have the literal character
.
.This indicates the text HELLO. will match while Hello. Nope.
In essence:
^[A-Z]+
: start with 1 or more capital letters[.]$
: that at the end of the text has a period (.
).I offer an option without using regular expressions, this will give your code less complexity