开发者

How to write regex to enforce maximum of 255 characters?

开发者 https://www.devze.com 2023-02-03 21:54 出处:网络
What regular expression would match any characters (including spaces), but have a maximum of 255 characters? 开发者_开发百科 Is this it?

What regular expression would match any characters (including spaces), but have a maximum of 255 characters? 开发者_开发百科 Is this it?

^[a-zA-Z0-9._]{1,255}$


Well, anything would be:

^.{1,255}$

. doesn't allow new lines. If that's a problem, you can use the dot-all flag (usually /s).

If you want to add spaces to your regex, try this (note the space):

  • ^[a-zA-Z0-9._ \t]{1,255}$ - Allow spaces and tabs.
  • ^[a-zA-Z0-9._\s]{1,255}$ - Allow all whitespaces.
  • ^[\s\w.]{1,255}$ - Same as the above (unless your flavor supports Unicode).


Well that would not allow anything, if you want anything, you're better off using ^.{1,255}$.

Or, if you want to allow nothing as well: ^.{0,255}$

0

精彩评论

暂无评论...
验证码 换一张
取 消