开发者

Allow Some Special Characters using Regex

开发者 https://www.devze.com 2023-04-06 06:21 出处:网络
The following regex matches any Unicode Letters 开发者_如何学编程+ Unicode Numbers + Vowel Signs + Dot + Dash + Underscore + Space

The following regex matches any Unicode Letters 开发者_如何学编程+ Unicode Numbers + Vowel Signs + Dot + Dash + Underscore + Space

/^[\w\pN\pL\pM .-]+$/u

Works successfully.

I want to edit my regex so it accepts the following:

? ! ( ) % @ # , + - : newline

- represents negative sign.

My attempt doesn't work:

/^[\w\pN\pL\pM .-**?!()%@#,+-:\r**]+$/u

Here is my snippet with latest attempt:

if(preg_match('/^[\w\pN\pL\pM .-?!()%@#,+-:\r]+$/u', $_POST['txtarea_msg']))

Any idea?


- is a metacharacter in character classes, so you're saying:

blahblahblah all characters from . to ? blahblahblah all characters from + to : blah blah

It needs to be escaped with a \: blahblah .\-? blahblah +\-: blahblah


/^[\w\pN\pL\pM \?!\(\)%@#,\+\-:\n\r]+$/u should do it.


Some of these are regular expression characters, so you need to escape them:

/^[\w\pN\pL\pM .?!()%@#,+\-:\r]+$/u

Also note the difference between a newline (\n) and carriage return (\r).

0

精彩评论

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