Code does not pass first validation
javascript, jquery, regex
Solution
You need to get rid of the quotes to make `regex` a regular expression literal:
var regex = /^[A-Za-z]+$/;
Here's an updated fiddle.
Currently, `regex` refers to a string literal. When you pass something that's not a regular expression object or literal to `String#match`, it implicitly converts that argument to a regular expression object (see ES5 15.5.4.10):
- If Type(regexp) is Object and the value of the [[Class]] internal property of regexp is `"RegExp"`, then let rx be regexp;
- Else, let rx be a new RegExp object created as if by the expression `new RegExp(` regexp`)` where `RegExp` is the standard built-in constructor with that name.
Your regular expression is therefore interpreted like this (since the string contains the forward slash characters you were expecting to delimit a regular expression literal):
var regex = /\/^[A-Za-z]+$\//;
That can't match anything, since it's looking for a forward slash, followed by the start of the string.
Problem
I have a function in which I am first checking that a string passed as argument has letters only or not. But it always return as false. Below is my jsfiddle ``` function takeString (str) { var regex = "/^[A-Za-z]+$/"; if (str.match(regex)) { if (str.charCodeAt(0) === str.toUpperCase().charCodeAt(0)) { alert('true'); return true; } else { alert('false'); return false; } } else { alert('Only letters please.'); } } takeString('string'); ``` The above code always alerts `Only letters please`.