RegEx在JavaScript中的最大长度

时间:2021-08-19 02:57:18

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?

我错过了什么?

4 个解决方案

#1


21  

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

你错过了收盘时的美元。正确的是:/^。{6,7} $/

#2


4  

Match the start and the end.

匹配开始和结束。

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

Your improved example

你改进的例子

#3


3  

You are missing the end anchor:

你错过了结束锚:

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

#4


3  

you must use end of string symbol $

你必须使用字符串符号$的结尾

like this ^.{6,7}$

像这样^。{6,7} $

#1


21  

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

你错过了收盘时的美元。正确的是:/^。{6,7} $/

#2


4  

Match the start and the end.

匹配开始和结束。

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

Your improved example

你改进的例子

#3


3  

You are missing the end anchor:

你错过了结束锚:

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

#4


3  

you must use end of string symbol $

你必须使用字符串符号$的结尾

like this ^.{6,7}$

像这样^。{6,7} $