[英]How to save pattern as ENV variable in environment.prod.ts?
I save a pattern in an ENV variable in environment.prod.ts
eg: 我将模式保存在
environment.prod.ts
中的ENV变量中,例如:
export const environment = { production: true, regExpTs : /^(?=.*[az])(?=.*[AZ])(?=.*\\d)(?=.*[#$@!%&*?])[A-Za-z\\d#$@!%&*?]{8,30}$/i, regExpHTML : '^(?=.*[az])(?=.*[AZ])(?=.*\\d)(?=.*[#$@!%&*?])[A-Za-z\\d#$@!%&*?]{8,30}$', };
The background of this idea is to be able to change the pattern when needed dynamically from the env and also to hide it from being embedded directly the HTML. 这个想法的背景是能够在需要时从env动态更改模式,并将其隐藏起来而不直接嵌入HTML。
The first variable: regExpTs
is being used in the *.ts
file and it's working fine. 第一个变量:
regExpTs
正在*.ts
文件中使用,并且工作正常。 The issue I face is regarding the variable: regExpHTML
. 我面临的问题是有关变量:
regExpHTML
。
I read the varaible in the *.ts
file as: let PASS_REGEXP_HTML = environment.regExpHTML;
我将
*.ts
文件中的变量读取为: let PASS_REGEXP_HTML = environment.regExpHTML;
and then I pass it as interpolation in the *.component.html
within the specific input field. 然后将其作为内插值传递到特定输入字段的
*.component.html
中。
Eg <input type="password" patter="{{PASS_REGEXP_HTML}}" ../>
The interpolation is also working fine, but the problem is that the pattern is somehow being changed and is't not the one saved in ENV any more. 例如,
<input type="password" patter="{{PASS_REGEXP_HTML}}" ../>
插值也可以正常工作,但是问题在于模式正在以某种方式更改,而不是保存在ENV中的模式更多。
It changes from: 它从:
^(?=.*[az])(?=.*[AZ])(?=.*\\d)(?=.*[#$@!%&*?])[A-Za-z\\d#$@!%&*?]{8,30}$
to: 至:
^(?=.*[az])(?=.*[AZ])(?=.*d)(?=.*[#$@!%&*?])[A-Za-zd#$@!%&*?]{8,30}$
First what's the reason that it's being changed and the second what would be the solution and/or the fix for such issue? 首先是要更改它的原因是什么,其次是该问题的解决方案和/或解决方法是什么?
It is because of backslash. 这是因为反斜杠。 Change your regExpHTML to following
将您的regExpHTML更改为
^(?=.*[az])(?=.*[AZ])(?=.*\\\\d)(?=.*[#$@!%&*?])[A-Za-z\\\\d#$@!%&*?]{8,30}$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.