用于 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
RegEx for maximum length in JavaScript
提问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
回答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}$