在常规的前pression限制字符长度 [英] Restricting Character length in Regular expression

查看:237
本文介绍了在常规的前pression限制字符长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的正前pression没有任何限制字符长度

I am using the following regular expression without restricting any character length

var test =  /^(a-z|A-Z|0-9)*[^$%^&*;:,<>?()\""\']*$/ //Works Fine

在当我试图将字符长度的限制为15,如下上面,它抛出一个错误。

In above when I am trying to restrict the characters length to 15 as below, it throws an error.

var test =  /^(a-z|A-Z|0-9)*[^$%^&*;:,<>?()\""\']*${1,15}/    //**Uncaught SyntaxError: Invalid regular expression**

请帮我作出上述正则表达式的工作与字符限制为15。

Please help me to make the above regEx work with the characters limit to 15.

推荐答案

您不能量词适用于锚。相反,以限制输入字符串的长度,使用开头挂靠一个前瞻:

You cannot apply quantifiers to anchors. Instead, to restrict the length of the input string, use a lookahead anchored at the beginning:

^(?=.{1,15}$)[a-zA-Z0-9]*[^$%^&*;:,<>?()\"']*$
  ^^^^^^^^^^^

另外,我假设你想匹配0个或多个字母或数字与(A-Z | A-Z | 0-9)* 。它应该看起来像 [A-ZA-Z0-9] * (即这里使用一个字符类)。

Also, I assume you wanted to match 0 or more letters or digits with (a-z|A-Z|0-9)*. It should look like [a-zA-Z0-9]* (i.e. use a character class here).

为什么不使用限制量词,如 {1,15} ,在结束了吗?

Why not use a limiting quantifier, like {1,15}, at the end?

量词仅施加于子模式的左侧,是它的一组或一个字符类,或者一个文字符号。因此, ^ [A-ZA-Z0-9] * [^ $%^&放大器; *;:?,&LT;&GT;()\\'] {1,15} $ 将有效地限制第二个字符长度类 [^ $%^&放大器; *;:?,&LT;&GT;()\\'] 来1到15个字符。在 ^([A-ZA-Z0-9] *?[^ $%^&放大器; *;:?,&LT;&GT;()\\'] *){} 1.15 $ 将限制的2子模式无限长度的序列(如 * (和 + ,太)可以匹配的字符数量不受限制),以1〜15倍,我们仍然不限制长度的整个输入字符串

Quantifiers are only applied to the subpattern to the left, be it a group or a character class, or a literal symbol. Thus, ^[a-zA-Z0-9]*[^$%^&*;:,<>?()\"']{1,15}$ will effectively restrict the length of the second character class [^$%^&*;:,<>?()\"'] to 1 to 15 characters. The ^(?:[a-zA-Z0-9]*[^$%^&*;:,<>?()\"']*){1,15}$ will "restrict" the sequence of 2 subpatterns of unlimited length (as the * (and +, too) can match unlimited number of characters) to 1 to 15 times, and we still do not restrict the length of the whole input string.

如何超前限制工作?

正向前查找 出现 ^ 启动的字符串的 。这是一个的零宽度断言的,只有返回如果它的子模式后续字符匹配检查之后真或假。所以,这个先行尝试匹配任何1到15(因 <强>限制量词 {1,15} )字符,但就在字​​符串的结尾换行符(由于 $ 锚)。如果我们从先行删除 $ 锚,超前只需要该字符串的包含的1到15个字符,但总字符串的长度是任何

The (?=.{1,15}$) positive lookahead appears right after ^ start-of-string anchor. It is a zero-width assertion that only returns true or false after checking if its subpattern matches the subsequent characters. So, this lookahead tries to match any 1 to 15 (due to the limiting quantifier {1,15}) characters but a newline right at the end of the string (due to the $ anchor). If we remove the $ anchor from the lookahead, the lookahead will only require the string to contain 1 to 15 characters, but the total string length can be any.

如果输入的字符串可以包含一个新行序列,你应该使用 [\\ S \\ S] 便携式任何字符的正则表达式结构(这将在JS和其他共同工作正则表达式口味):

If the input string can contain a newline sequence, you should use [\s\S] portable any-character regex construct (it will work in JS and other common regex flavors):

^(?=[\s\S]{1,15}$)[a-zA-Z0-9]*[^$%^&*;:,<>?()\"']*$
 ^^^^^^^^^^^^^^^^^

这篇关于在常规的前pression限制字符长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆