问题
How can I limit the length of a string matching a RegEx
I assumed that var sixCharsRegEx = /^.{6,7}/
would only match strings of lengths 6 or 7
but no: http://jsfiddle.net/FEXbB/
What am I missing?
回答1:
You are missing closing dollar at the end. Correct one is: /^.{6,7}$/
回答2:
Match the start and the end.
var sixCharsRegEx = /^.{6,7}$/;
Your improved example
回答3:
you must use end of string symbol $
like this ^.{6,7}$
回答4:
You are missing the end anchor:
var sixCharsRegEx = /^.{6,7}$/
来源:https://stackoverflow.com/questions/11511154/regex-for-maximum-length-in-javascript