如何在正则表达式中允许双引号?

Man*_*der 0 regex asp.net

我有一个文本框:

<asp:RegularExpressionValidator ID="ValidateTitleCharacters" runat="server" 
    ValidationExpression="^[a-zA-Z0-9@+'.!#$',:;=/\(\),\-\s]{1,255}$"
    ControlToValidate="title" Text="You have entered a character(s) that is not allowed in the title."
    ErrorMessage="You have entered a character(s) that is not allowed in the title." />
Run Code Online (Sandbox Code Playgroud)

我想允许"字符也.我怎么能修改正则表达式字符串???

我试过这个:

<asp:RegularExpressionValidator ID="ValidateTitleCharacters" runat="server" 
    ValidationExpression="^[a-zA-Z0-9@+'.!#$'\",:;=/(),\-\s]{1,255}$"
    ControlToValidate="title" Text="You have entered a character(s) that is not allowed in the title."
    ErrorMessage="You have entered a character(s) that is not allowed in the title." />

<asp:RegularExpressionValidator ID="ValidateTitleCharacters" runat="server" 
    Validat??ionExpression="^[a-zA-Z0-9@+'.!#$',:;=/()(""),\-\s]{1,255}$"
    ControlToValidate="title" Text="You have entered a character(s) that is not allowed in the title."
    ErrorMessage="You have entered a character(s) that is not allowed in the title." />
Run Code Online (Sandbox Code Playgroud)

两次尝试都破坏了字符串.

Ode*_*ded 9

从您发布的片段中可以看出,正则表达式嵌入在标记中 - 这意味着您需要将双引号字符转义为HTML字符实体.

用途&quot;:

ValidationExpression="^[a-zA-Z0-9@+'.!#$'&quot;,:;=/\(\),\-\s]{1,255}$"
Run Code Online (Sandbox Code Playgroud)

ASP.NET引擎将字符实体转换为".

或者,ValidationExpression在代码后面设置值(OnInit例如):

ValidateTitleCharacters.ValidationExpression = 
                                  "^[a-zA-Z0-9@+'.!#$'\",:;=/\(\),\-\s]{1,255}$";
Run Code Online (Sandbox Code Playgroud)