简体   繁体   中英

Allowing question mark with alphanumeric and spaces with other characters in regex

I am using PHP preg_match() to check a regex list. The function is checking for alphanumeric, spaces, underscores, dashes, asterisk and question mark. But it only takes all the above except question mark and asterisk.

return (bool) preg_match('/^[a-z0-9 _-]+$/i', $str);

The function is check for alphanumeric, spaces, underscore, dashes, asterix and question mark, but it only takes all the above except question mark and asterix

Because there is no ? or * in your character class . Try

return (bool) preg_match('/^[\w*? -]+$/', $str);

\\w already contains [A-Za-z0-9_]

See demo at regex101

这是因为您将需要转义其他字符,因为它们是保留的特殊字符。

return (bool) preg_match('/^[a-z0-9 _-\?\*]+$/i', $str);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM