Regex allows only 1 character

后端 未结 6 1438
长情又很酷
长情又很酷 2021-01-22 03:50
$rex = \'/^[^<,\"@?=>|;#]$/i\';

I\'m having trouble with this regular expression. The idea is that the input fields are checked for certain chara

6条回答
  •  执念已碎
    2021-01-22 04:04

    What you probably actually need is:

    $rex = '/[<,"@$?=>|;#]/';
    

    Then your error case is when this regex matches, not when it doesn't.

    This is equivalent to doing what you're currently doing with this small change to your regex:

    $rex = '/^[^<,"@?=>|;#]*$/i';
    

    This is kind of nonsensically overcomplex, though. Like trying to find out whether there's an elephant in the room by counting how many things in the room aren't elephants, then seeing if that number is the same as the number of things in the room. (And the /i modifier is not, at any time, accomplishing anything.)

提交回复
热议问题