简体   繁体   English

为什么我的正则表达式可以在.NET中工作,而不能在Javascript中工作?

[英]Why does my regex work in .NET but not in Javascript?

I am using an asp RegularExpressionValidator to validate if a textarea has html or encoded html. 我正在使用asp RegularExpressionValidator来验证textarea是否具有html或已编码的html。 I need the validator to work client side because I have ValidateRequest set to true on the page. 我需要验证器来工作客户端,因为我在页面上将ValidateRequest设置为true。 My regex is set to match any string that does not have a less than character followed by an alpha character or an ampersand followed by some number of alpha characters ending in a semi-colon. 我的正则表达式设置为匹配任何不小于字符的字符串,后跟一个字母字符或一个&符号,后跟一些以分号结尾的字母字符。

^((?![<]{1}[a-z]{1}).)*$
^((?![&]{1}[a-z]+;).)*$

Javascript does not have a concept of Single-Line which lets your period match any character including line breaks. Javascript没有单行的概念,它使您的句点与任何字符匹配,包括换行符。 You should use the following in place of your comma: [\\s\\S] 您应该使用以下内容代替逗号:[\\ s \\ S]

^((?![<]{1}[a-z]{1})[\s\S])*$
^((?![&]{1}[a-z]+;)[\s\S])*$

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

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