RegEx for maximum length in JavaScript

时光怂恿深爱的人放手 提交于 2021-01-02 04:51:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!