douzhaocheng4533 2016-05-04 15:37
浏览 37
已采纳

使用XRegExp进行Yii2验证,\ p {L}无效

I'm using Yii2 framework and I have a Validator that should do client-side validation. I have a regex that looks like this: /^[\\p{L}]+$/u for simplicity, but my actual regex is a bit more complicated, but the \p{L} part is what causes the problems.

And so my code like this in the validator class:

public function clientValidateAttribute($model, $attribute, $view)
{
    $message = json_encode($this->message, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
    return <<<JS
    if (!XRegExp('/^[\\p{L}]+$/u').test(value)) {
        messages.push($message);
    }
JS;
}

Problem is, this always fails for \p{L} but if I change the pattern for something like /^[A-Z]+$/ it works flawlessly.

I'm using the 1.3.0 xregexp-all.js. It is added to in an AssetBundle class in \assets\AppAsset.php

  • 写回答

1条回答 默认 最新

  • dongshu4755 2016-05-05 13:33
    关注

    I did notice while I was playing around with my regex, that when I made it wrong and was shown an exception, \\p{L} was interpreted as p{L}. So after some tries I've found out that for whatever reason I needed four backslashes to get it interpreted as \p{L}. Final code that works:

    public function clientValidateAttribute($model, $attribute, $view)
    {
        $message = json_encode($this->message, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        return <<<JS
        if(!XRegExp('^[\\\\p{L}]+$').test(value)) {
            messages.push($message);
        }
    JS;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了