简体   繁体   English

Prettier 移除 Angular 中的转义字符

[英]Prettier removes escape characters in Angular

I am using Prettier in my Angular project, and I'm trying to include a regex pattern for a form validator.我在我的 Angular 项目中使用 Prettier,并且我正在尝试为表单验证器包含一个正则表达式模式。 When I run Prettier, the string is fundamentally altered to make the pattern validation dysfunctional, as shown here:当我运行 Prettier 时,字符串会从根本上改变以使模式验证功能失调,如下所示:

Before:前:

export const EmailVal: ScaffoldValidator = PatternVal(
  "/^[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z_+])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}$/"
);

After:后:

export const EmailVal: ScaffoldValidator = PatternVal(
  "/^[0-9a-zA-Z]([-.w]*[0-9a-zA-Z_+])*@([0-9a-zA-Z][-w]*[0-9a-zA-Z].)+[a-zA-Z]{2,9}$/"
);

I've gone through the Prettier Docs, which speaks to changing escapes within the context of single vs double quotes, yet asserts that other escapes will be untouched.我已经阅读了 Prettier Docs,它谈到了在单引号和双引号的上下文中更改转义,但断言其他转义将保持不变。 I've also gone through my tsLint.json , and nothing stands out to me as the source.我也浏览了我的tsLint.json ,没有什么对我来说很突出。 It does denote "Codelyzer" as the ruleset.它确实将“Codelyzer”表示为规则集。 Is there a setting somewhere I can disable to prevent this behavior?我可以禁用某个设置以防止这种行为吗?

You can directly use a regex (note: no quotes - just the slashes):您可以直接使用正则表达式(注意:没有引号 - 只有斜杠):

/^[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z_+])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}$/;

or correct the string escapes (note the double backslashes):或更正字符串转义(注意双反斜杠):

"/^[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z_+])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9}$/";
                  ^^                                 ^^              ^^  

Consider using EsLint - it can warn you about these cases (see no-useless-escape rule ):考虑使用 EsLint - 它可以警告您这些情况(请参阅no-useless-escape rule ): 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM