简体   繁体   中英

Regex that works in JS not working in PHP

This regexp:

/^admin$|^[А-Яа-я]{2,20}\s[А-Яа-я]{2,20}\s[А-Яа-я]{2,20}$/

should match this string:

Пупкин Василий Иванович

It does match in JS (tested in Firebug console) and doesn't in PHP, and I cannot understand why.

In JavaScript, strings are always UTF-8. In PHP, however, they are a sequence of bytes. In order to allow PHP's regex engine to handle UTF-8 strings, add the u modifier to the regex.

You can also simplify your regex by removing all the uppercase letter ranges and using the i modifier, however this may be locale-dependent so be sure to test it!

For more on modifiers, check the docs !

You might want to use:

\p{Cyrillic}

Read Unicode character properties for more information.

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