So, let's say I need a single regular expression to both check for a pattern match within a string and to verify that the total length of the string does not exceed a given number of characters. Yes, I know that it would be better to check the length with a separate call to length, but in this case I need one regex and nothing more.
Here's what I came up with, but I was curious as to whether there was a better way to do this. This example assumes that you need a string with the word "foo" somewhere in it, but the total length of the string itself can't be more than 50 characters.
/^(?=.{0,50}$).*foo/i-jehuni
In reply to validating string length with a regular expression by jehuni
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |