Javascript 如何使用正则表达式检查给定值中的最少 3 个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4630908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:20:55  来源:igfitidea点击:

How can check a minimum 3 characters in a given value ,using regular expression

javascriptregex

提问by Linto P D

I have javascript code for checking the zipcode

我有用于检查邮政编码的 javascript 代码

var regexObj =
/^(?=[^-]*-?[^-]*$)[0-9-]*[0-9]$/;

I need to add one more condition to this,ie

我需要再添加一个条件,即

make it so that user has to enter a minimum of 3 characters

使用户必须输入至少 3 个字符

Can any one say, how can i modify my regular expression for that

任何人都可以说,我该如何修改我的正则表达式

回答by moinudin

/^(?=[^-]*-?[^-]*$)[0-9-]*[0-9]$/

is equivalent to

相当于

/^[0-9-]*[0-9]$/

You can add a length check in the same pass without requiring lookahead

您可以在同一遍中添加长度检查而无需前瞻

/^[0-9-]{2,}[0-9]$/

That's a minimum of 3 characters, the last being a digit and the rest being digits and -. See http://www.rubular.com/r/oa9wVxggz0

这至少是 3 个字符,最后一个是数字,其余是数字和-。见http://www.rubular.com/r/oa9wVxggz0

You might also want to restrict the first character from being a -. You might also want to require 3 digits, without counting the -as one of the required 3 characters. Putting these together we would get:

您可能还想限制第一个字符为-. 您可能还需要 3 个数字,而不将 计-为所需的 3 个字符之一。把这些放在一起我们会得到:

/^[0-9]-*[0-9][0-9-]*[0-9]$/

See http://www.rubular.com/r/Qhl843Txib

http://www.rubular.com/r/Qhl843Txib

回答by Matt Ball

Why in the world do you need a regex to check the length of a string?

为什么你需要一个正则表达式来检查字符串的长度?

var testString1 = 'this is long enough';
alert(testString1.length >= 3); // true

var testString2 = 'no';
alert(testString2.length >= 3); // false

回答by thecoolmacdude

In HTML5, you can use a pattern.

在 HTML5 中,您可以使用模式

In your example, you would do the following, where 3 is the minimum value and anything after the comma would be a maximum value (in your case, you don't have a maximum value):

在您的示例中,您将执行以下操作,其中 3 是最小值,逗号之后的任何内容都是最大值(在您的情况下,您没有最大值):

<input pattern=".{3,}">

回答by thecoolmacdude

Are you sure this is for checking "ZipCode" (uniquely American)?
Wikipedia: "ZIP codes are a system of postal codes used by the United States Postal Service (USPS) since 1963. The term ZIP, an acronym for Zone Improvement Plan,[1] is properly written in capital letters and was chosen to suggest that the mail travels more efficiently, and therefore more quickly, when senders use the code in the postal address. The basic format consists of five decimal numerical digits. An extended ZIP+4 code, introduced in the 1980s, includes the five digits of the ZIP code, a hyphen, and four more digits that determine a more precise location than the ZIP code alone."
/^(\d{5})(-\d{4})?$/

你确定这是为了检查“邮政编码”(美国独有的)?
维基百科:“邮政编码是美国邮政局 (USPS) 自 1963 年以来使用的邮政编码系统。ZIP 一词是 Zone Improvement Plan 的首字母缩写词,[1] 正确地用大写字母书写,并被选择以表明当发件人使用邮政地址中的代码时,邮件的传输效率更高,因此速度更快。基本格式由五个十进制数字组成。1980 年代引入的扩展 ZIP+4 代码包括邮政编码的五个数字代码、一个连字符和另外四位数字来确定比单独的邮政编码更精确的位置。”
/^(\d{5})(-\d{4})?$/