正则表达式仅适用于长度 - 任何字符

时间:2022-02-11 21:40:55

I'm trying to regex the contents of a textarea to be between 4 and 138 characters.

我正在尝试将textarea的内容复制到4到138个字符之间。

My regular expression is this: '/^.{4,138}$/'

我的正则表达式如下:'/^。{4,138} $ /'

But - I want the user to be able to include return characters, which I believe the "." keeps them from doing. Thoughts on what I need to change?

但是 - 我希望用户能够包含返回字符,我相信“。”阻止他们这样做。关于我需要改变什么的想法?

EDIT :

编辑:

Obviously there are other ways to get the length of text - strlen...etc, but I'm looking for regex specifically due to the nature of the check (ie a plugin that requires regex)

显然还有其他方法来获取文本的长度 - strlen ...等,但我正在寻找正则表达式,因为检查的性质(即需要正则表达式的插件)

4 个解决方案

#1


7  

I want the user to be able to include return characters, which I believe the "." keeps them from doing. Thoughts on what I need to change?

我希望用户能够包含返回字符,我相信“。”阻止他们这样做。关于我需要改变什么的想法?

Either:

或者:

  • change the . to [\s\S] (whitespace/newlines are part of \s, all the rest is part of \S)
  • 改变了。到[\ s \ S](空格/换行符是\ s的一部分,其余部分都是\ S的一部分)
  • use the SINGLE_LINE (a.k.a DOTALL) regex flag /…/s
  • 使用SINGLE_LINE(a.k.a DOTALL)正则表达式标志/ ... / s

#2


6  

Why don't you just check the string length using strlen? It would be much more efficient than doing a regex. Plus, you can give meaningful error messages.

你为什么不用strlen检查字符串长度?它比做正则表达式更有效率。另外,您可以提供有意义的错误消息。

$length = strlen($input);

if ($length > 138)
   print 'string too long';
else if ($length < 4)
   print 'string too short';

#3


2  

Either

/^.{4,138}$/s

or

要么

/^[\s\S]{4,138}$/ 

will match newlines.

将匹配换行符。

The s flag tells the engine to treat the whole thing as a single line, so . will match \n in addition to what it usually matches. Note that this also causes ^ and $ to match at the beginning and end of the entire string (rather than just the beginning/end of each line) unless you also use the m flag.

s标志告诉引擎将整个事物视为一行,所以。将匹配\ n除了通常匹配的内容。请注意,这也会导致^和$在整个字符串的开头和结尾(而不仅仅是每行的开头/结尾)匹配,除非您还使用m标志。

Here's some more info on regex flags.

这里有关于正则表达式标志的更多信息。

#4


1  

Try '/^.{%d,%d}$/s' to have . match newlines as well.

试试'/^.{%d,%d}$/s'即可。也匹配换行符。

#1


7  

I want the user to be able to include return characters, which I believe the "." keeps them from doing. Thoughts on what I need to change?

我希望用户能够包含返回字符,我相信“。”阻止他们这样做。关于我需要改变什么的想法?

Either:

或者:

  • change the . to [\s\S] (whitespace/newlines are part of \s, all the rest is part of \S)
  • 改变了。到[\ s \ S](空格/换行符是\ s的一部分,其余部分都是\ S的一部分)
  • use the SINGLE_LINE (a.k.a DOTALL) regex flag /…/s
  • 使用SINGLE_LINE(a.k.a DOTALL)正则表达式标志/ ... / s

#2


6  

Why don't you just check the string length using strlen? It would be much more efficient than doing a regex. Plus, you can give meaningful error messages.

你为什么不用strlen检查字符串长度?它比做正则表达式更有效率。另外,您可以提供有意义的错误消息。

$length = strlen($input);

if ($length > 138)
   print 'string too long';
else if ($length < 4)
   print 'string too short';

#3


2  

Either

/^.{4,138}$/s

or

要么

/^[\s\S]{4,138}$/ 

will match newlines.

将匹配换行符。

The s flag tells the engine to treat the whole thing as a single line, so . will match \n in addition to what it usually matches. Note that this also causes ^ and $ to match at the beginning and end of the entire string (rather than just the beginning/end of each line) unless you also use the m flag.

s标志告诉引擎将整个事物视为一行,所以。将匹配\ n除了通常匹配的内容。请注意,这也会导致^和$在整个字符串的开头和结尾(而不仅仅是每行的开头/结尾)匹配,除非您还使用m标志。

Here's some more info on regex flags.

这里有关于正则表达式标志的更多信息。

#4


1  

Try '/^.{%d,%d}$/s' to have . match newlines as well.

试试'/^.{%d,%d}$/s'即可。也匹配换行符。