开发者

Is this Regular expression for Javascript is correct

Iam looking for the user to enter 3 characters and they should be letters and not numbers and special开发者_运维百科 characters

 if(txt1.match('[A-Za-z]{3}')){}

Is the above if condition is correct ?


You can use the i regex flag to make the matching case insensitive and simplify the regex. Also, without ^ and $, any string containing three consecutive letters will match. You need to add ^ and $ to match the beginning and end of the string respectively. Finally, if you don't need to capture the text matched, you should use the test() method of the regular expression object to get a simple Boolean value:

if ( /^[a-z]{3}$/i.test(txt1) ) {}


Nope, you haven't used the correct delimiters for a regular expression, e.g. /reg/:

if(txt1.match(/[A-Za-z]{3}/)){}

Although match() will construct a regex from any string passed, you might confuse yourself when you need to start escaping characters.

You also need start and end anchors, ^ and $ respectively, to ensure that the regular expression matches from the start of the string to the end of the string:

if(txt1.match(/^[A-Za-z]{3}$/)){}

Finally, if you only want a true or false check (in this case you do), use test() instead of match():

if(/^[A-Za-z]{3}$/.test(txt1)){}

test() is a method on instances of regular expressions, so the regex comes before the function call.


That would match against any string with 3 or more characters, to limit it to strictly 3 alphabets, try

if(txt1.match('^[A-Za-z]{3}$')){}


Very close

if(txt1.match(/[A-Za-z]{3}/)) {}

Note: the above will match if the user enters 3 characters that are letters and not number or special characters. It will also match if the user enters something like

234abc098

If you want to ensure that it is only 3 characters total then make sure to put the beginning of string and end of string markers in the regex

^[A-Za-z]{3}$


if (txt1.match(/^[A-Za-z]{3}$/)) { }

I changed 2 things. First A regex in Javascript starts and ends with a slash. Second, I added a start and end character (^ and $). This ensures that the stat of the string is followed by 3 letters, and then ends. Meaning it must be exactly 3 letters.


No. First, the syntax is wrong, it should use a regex literal (actually, a string seems to work here too):

txt1.match(/[A-Za-z]{3}/)

Next, if you want 3 character and not more, make sure to include the start and end anchors:

txt1.match(/^[A-Za-z]{3}$/)


Javascript has a special syntax for regular expressions: /regex/. The following shows the correct implementation of regular expressions:

if(txt1.match(/[A-Za-z]{3}/)){}

There are several RE (regular expression) functions available, on the String object (like "string") and the RegExp object (like /regex/). If you just want to test a string against a regexp without saving the result, it's recommended to use the RegExp.test function:

if(/^[A-Za-z]{3}$/.test(txt1)){}

Note: I've added ^ and $ because it should match all of the characters, containing 3 characters is not enough. ^ indicates the beginning of the string, $ the end.


First of all, you have to use forward slashes instead of single quotes to directly pass a regular expression to match() in javascript.

Also, what you're currently checking with your expression is, if 3 letters appear in the checked string consecutively. If you want to make sure that the user just enters 3 letters and nothing else, also check for the beginning (^)and end ($) of the string with your regular expression. The complete expression looks like this:

if(txt1.match(/^[A-Za-z]{3}$/)){}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜