将字符数限制为 10 的正则表达式 [英] Regular expression to limit number of characters to 10

查看:62
本文介绍了将字符数限制为 10 的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 正则表达式,它只允许小写字母和最多 10 个字母人物.我到目前为止看起来像这样:

I am trying to write a regular expression that will only allow lowercase letters and up to 10 characters. What I have so far looks like this:

pattern: /^[a-z]{0,10}+$/ 

这不起作用或无法编译.我有一个只允许小写字母的工作,它是这样的:

This does not work or compile. I had a working one that would just allow lowercase letters which was this:

pattern: /^[a-z]+$/ 

但我需要将字符数限制为 10.

But I need to limit the number of characters to 10.

推荐答案

您可以使用花括号来控制出现的次数.例如,这表示 0 到 10:

You can use curly braces to control the number of occurrences. For example, this means 0 to 10:

/^[a-z]{0,10}$/

选项是:

  • {3} 正好 3 次;
  • {6,} 至少出现 6 次;
  • {2,5} 出现 2 到 5 次.

请参阅正则表达式参考.

你的表达式在右花括号后面有一个 +,因此是错误的.

Your expression had a + after the closing curly brace, hence the error.

这篇关于将字符数限制为 10 的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆