I am using regex to verify that a string only contains alphabets and spaces. Regex is defined as
var regex = /^[A-Za-z ]/;
but even if I am testing it with a string “X,” , it is giving a true result.
What is the error here?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
^[A-Za-z ]only matches one character. and^means the start of the string. To accomplish what you want, use:+– This means match one or more. Alternatively you can use:*– Which means match zero or more.But I think you’re better off with the first one (
+). Another thing is, match for the whole string. This means you have to search from the first to the last character.$– This means match the end.Your code should be like this: