简体   繁体   English

这个Java正则表达式正确吗?

[英]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 Iam寻找用户输入3个字符,并且应为字母,而不是数字和特殊字符

 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. 您可以使用i 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: 最后,如果不需要捕获匹配的文本,则应使用正则表达式对象的test()方法来获取简单的布尔值:

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

Nope, you haven't used the correct delimiters for a regular expression, eg /reg/ : 不,您没有为正则表达式使用正确的分隔符,例如/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. 尽管match()将根据传递的任何字符串构造一个正则表达式,但是当您需要开始转义字符时,您可能会感到困惑。

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() : 最后,如果您只想进行truefalse检查(在这种情况下,您需要这样做),请使用test()而不是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. test()是正则表达式实例的一种方法,因此正则表达式位于函数调用之前。

它将与任何具有3个或更多字符的字符串匹配,以将其严格限制为3个字母,请尝试

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. 注意:如果用户输入3个字母,而不是数字或特殊字符,则上述内容将匹配。 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 如果要确保总共只有3个字符,请确保将正则表达式中的字符串开头和字符串末尾放入

^[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. 首先,Javascript中的正则表达式以斜杠开头和结尾。 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. 这样可以确保字符串的统计信息后面跟随3个字母,然后结束。 Meaning it must be exactly 3 letters. 表示它必须正好是3个字母。

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: 接下来,如果您需要3个字符或更多,请确保包括开始和结束锚点:

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

Javascript has a special syntax for regular expressions: /regex/ . Javascript对于正则表达式具有特殊的语法: /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/ ). 在String对象(例如"string" )和RegExp对象(例如/regex/ )上有几种RE(正则表达式)函数可用。 If you just want to test a string against a regexp without saving the result, it's recommended to use the RegExp.test function: 如果只想针对一个regexp测试字符串而不保存结果,建议使用RegExp.test函数:

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. 注意:我添加了^$因为它应该匹配所有字符,仅3个字符是不够的。 ^ 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. 首先,您必须使用正斜杠而不是单引号将正则表达式直接传递给javascript中的match()。

Also, what you're currently checking with your expression is, if 3 letters appear in the checked string consecutively. 另外,您当前正在使用表达式检查的是,如果连续3个字母出现在选中的字符串中。 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. 如果要确保用户仅输入3个字母,不要输入其他任何字符,请同时使用正则表达式检查字符串的开头( ^ )和结尾( $ )。 The complete expression looks like this: 完整的表达式如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM