用于 JavaScript 中最大长度的 RegEx

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11511154/
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-24 06:05:05  来源:igfitidea点击:

RegEx for maximum length in JavaScript

javascriptregexmaxlength

提问by EasierSaidThanDone

How can I limit the length of a string matching a RegEx

如何限制与 RegEx 匹配的字符串的长度

I assumed that var sixCharsRegEx = /^.{6,7}/would only match strings of lengths 6 or 7

我认为var sixCharsRegEx = /^.{6,7}/只会匹配长度为 6 或 7 的字符串

but no: http://jsfiddle.net/FEXbB/

但没有:http: //jsfiddle.net/FEXbB/

What am I missing?

我错过了什么?

回答by Arkadiusz 'flies' Rzadkowolski

You are missing closing dollar at the end. Correct one is: /^.{6,7}$/

你最后错过了收盘美元。正确的一种是:/^.{6,7}$/

回答by Madara's Ghost

Match the start and the end.

匹配开始和结束。

var sixCharsRegEx = /^.{6,7}$/;

Your improved example

你改进的例子

回答by Esailija

You are missing the end anchor:

你错过了结束锚点:

var sixCharsRegEx = /^.{6,7}$/

回答by burning_LEGION

you must use end of string symbol $

您必须使用字符串结尾符号 $

like this ^.{6,7}$

像这样 ^.{6,7}$