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
I was trying to exclude ' and " in a ng-pattern which is similar to the problem above. I found a way to embed ' or " directly without having to use a scope variable:
<input type="text" ng-pattern="/^[^"']+$/" />
First of all, your regex contains too many escaping symbols, while you only need to escape the
"
here and\\
.Then, to match a
"
insideng-pattern
attribute, you may define it as\x22
or"
: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 theng-pattern
attribute. Note that to match a literal\
you need to use 4 backslashes in the regex pattern.You can use
\"
to escape"
.