doutong7216 2014-03-25 10:58
浏览 43
已采纳

在JS中工作的正则表达式不适用于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.

  • 写回答

2条回答 默认 最新

  • doufei2355 2014-03-25 11:02
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?