Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for length only - any characters

Tags:

regex

php

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

My regular expression is this: '/^.{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)

like image 587
Dave Avatar asked Jun 09 '11 16:06

Dave


1 Answers

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)
  • use the SINGLE_LINE (a.k.a DOTALL) regex flag /…/s
like image 56
Tomalak Avatar answered Sep 26 '22 08:09

Tomalak