使用正则表达式检查字符串长度

Regular expressions are the ace up the sleeve of many of the most talented programmers out there. With a solid understanding of how regular expressions work, it’s sometimes possible to bypass a tangled mess of conditional logic and recursive function calls with a simple one-liner that just gets the job done much more efficiently.

正则表达式是许多最有才华的程序员的王牌。 有了对正则表达式的工作原理的深刻理解,有时可以使用简单的单行代码绕开缠结的条件逻辑和递归函数调用,从而更有效地完成工作。

One of the convenient places to use regular expressions is when doing form validation. With HTML5 it’s possible to enforce attributes like field length, but if someone tries to mess with your security and post values without using the form, you need to be able to verify that what you intended and what you’re getting are consistent.

使用正则表达式的便利位置之一是进行表单验证时。 使用HTML5,可以强制执行诸如字段长度之类的属性,但是如果有人试图破坏您的安全性并在不使用表单的情况下发布值,则需要能够验证您的意图和所获得的内容是否一致。

To check the length of a string, a simple approach is to test against a regular expression that starts at the very beginning with a ^ and includes every character until the end by finishing with a $. You specify the number of characters you want to accept by putting that value inside curly braces right before the ‘$’ at the end.

要检查字符串的长度,一种简单的方法是对正则表达式进行测试,该正则表达式以^开头,并以$结束,包括每个字符直到结尾。 您可以通过将值放在大括号内的$后面来指定要接受的字符数。

确切长度 (Exact Length)

For example, if I wanted a regular expression that had to consist of exactly seven letters, I might write a regular expression like this:

例如,如果我想要一个必须由七个字母组成的正则表达式,则可以编写如下正则表达式:

/^[a-zA-Z]{7}$/

This will match a seven-letter string like ‘abcdefg’ but not any string longer or shorter than seven letters, and certainly not any string containing anything else but letters.

这将匹配七个字母的字符串,例如“ abcdefg”,但不会匹配任何长于或短于七个字母的字符串,当然也不会匹配除字母以外的任何其他字符串。

长度范围 (Length Range)

It might be more sensible for real users if I also included a lower limit on the number of letters. To do this, just add a lower value and a comma before the upper limit inside the curly braces:

如果我还对字母的数量设置了下限,则对实际用户可能更明智。 为此,只需在花括号内的上限之前添加一个较低的值和一个逗号:

/^[a-zA-Z]{3,7}$/

Now any set of letters between three and seven letters will be matched.

现在,三到七个字母之间的任何一组字母都将匹配。

最小长度 (Minimum Length)

If, on the other hand, I just wanted to set the lower limit, I could leave off the second value, as long as I included the comma:

另一方面,如果我只想设置下限,则可以省略第二个值,只要包含逗号即可:

/^[a-zA-Z]{3,}$/

Now a string of at least three letters, but extending to any length, and containing nothing other than letters, will be matched. Depending on your language, that could be a complicated proposition to test for without the convenience of regular expressions.

现在,将匹配至少三个字母,但可以扩展到任意长度并且不包含字母的字符串。 如果没有正则表达式的便利,根据您的语言,这可能是一个复杂的命题。

Learn how to use regular expressions in your coding, and you will start discovering a wide range of useful applications. They may look cryptic at first, but becoming adept with regular expressions will pay off in the long run.

了解如何在编码中使用正则表达式,您将开始发现各种有用的应用程序。 乍一看,它们可能看起来有些晦涩难懂,但从长远来看,熟练使用正则表达式将有回报。

翻译自: https://www.sitepoint.com/using-regular-expressions-to-check-string-length/

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值