ng-pattern with regex having double quotes does not escape correctly

后端 未结 3 1420
后悔当初
后悔当初 2021-01-18 20:36

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

3条回答
  •  滥情空心
    2021-01-18 20:56

    First of all, your regex contains too many escaping symbols, while you only need to escape the " here and \\.

    Then, to match a " inside ng-pattern attribute, you may define it as \x22 or ":

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

    Enter text to validate:

    Text doesn't match with ng-pattern!

    You may also solve the problem by defining a regex in the controller with a regular string literal where you may use '.."..' or "..\"...", and then use the variable name inside {{...}} in the ng-pattern attribute. 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}$/";
    });
    
    
    Please enter a file name. Please enter a valid file name.

提交回复
热议问题