NG-模式具有正则表达式的双引号不能正确逃生 [英] ng-pattern with regex having double quotes does not escape correctly

查看:417
本文介绍了NG-模式具有正则表达式的双引号不能正确逃生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NG-模式验证为一个正则表达式^ [^ \\ ./:* \\ \\<> \\ | {1} [^ \\ /?:* \\ \\?]<> \\ | ] {} 0.254 $基本上在测试和文件路径限制德无效字符。但是当我有指定为NG-模式

  NG-模式=^ [^ \\\\\\ ./:\\ * \\ \\?<> \\ |] {1} [^ \\\\ /:\\ * ?\\ \\<> \\ |] {} 0.254 $

中,NG-模式显示了一个不正确的方式正则表达式。上正确地实现这一目标的任何帮助。


解决方案

您正则表达式包含了太多逃脱的符号,而你只需要逃避在这里和 \\\\

既然你有一个双引号,您应该声明在控制器正则表达式,然后使用变量名内 {{...}} NG-模式属性。

另外,还要注意要匹配文字 \\ 你需要在正则表达式使用4个反斜线。

\r
\r

VAR应用= angular.module(应用程序,[]);\r
\r
app.controller(FormCtrl功能($范围){\r
  $ scope.regex ​​=/^[^\\\\\\\\./:*?\\\"<>|][^\\\\\\\\/:*?\\\"<>|]{0,254}$/ \r
});

\r

&LT;脚本SRC =htt​​ps://ajax.googleapis.com/ajax /libs/angularjs/1.2.23/angular.min.js\"></script>\r
&LT; D​​IV NG-应用=应用程序&GT;\r
    &LT;表格名称=theFormNG控制器=FormCtrlNOVALIDATE&GT;\r
        &LT;输入类型=文本名称=文件名placholder =文件名NG-模式=文件名NG-模式={{正则表达式}}要求/&GT;\r
        &LT; D​​IV CLASS =错误\r
                     纳克秀=(theForm.filename $脏||企图。)及与放大器; theForm.filename $无效&GT;\r
                  &LT;小班=的错误文字危险\r
                         NG-秀=$ theForm.filename error.required。&GT;\r
                    请输入一个文件名。\r
                  &LT; /小&GT;\r
                  &LT;小班=的错误文字危险\r
                         NG-秀=$ theForm.filename error.pattern。&GT;\r
                    请输入一个有效的文件名。\r
                  &LT; /小&GT;\r
                &LT; / DIV&GT;\r
    &LT; /表及GT;\r
&LT; / DIV&GT;

\r

\r
\r

I have a ng-pattern validation for a regex of ^[^\./:*\?\"<>\|]{1}[^\/:*\?\"<>\|]{0,254}$ which basically tests the invalid chars in filepath and teh limit. but when i have the ng-pattern specified as

 ng-pattern = "^[^\\\./:\*\?\"<>\|]{1}[^\\/:\*\?\"<>\|]{0,254}$"

, the ng-pattern shows the regex in an incorrect way. any help on achieving this correctly

解决方案

Your regex contains too many escaping symbols, while you only need to escape the " here and \\.

Since you have a double quote, you should declare the regex in the controller, and then use the variable name inside {{...}} in the ng-pattern attribute.

Also, note that to match a literal \ you need to use 4 backslashes in the regex pattern.

var app = angular.module("app",[]);

app.controller("FormCtrl", function($scope) {
  $scope.regex = "/^[^\\\\./:*?\"<>|][^\\\\/:*?\"<>|]{0,254}$/";
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
    <form name="theForm" ng-controller="FormCtrl" novalidate>
        <input type="text" name="filename" placholder="filename" ng-model="filename" ng-pattern="{{regex}}" required />
        <div class="error"
                     ng-show="(theForm.filename.$dirty || attempted) && theForm.filename.$invalid">
                  <small class="error text-danger"
                         ng-show="theForm.filename.$error.required">
                    Please enter a file name.
                  </small>
                  <small class="error text-danger"
                         ng-show="theForm.filename.$error.pattern">
                    Please enter a valid file name.
                  </small>
                </div>
    </form>
</div>

这篇关于NG-模式具有正则表达式的双引号不能正确逃生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆