我需要一个正则表达式来验证一个大文本(最多2000个字符),它应该按如下方式工作:
说n = 20
我现在正在使用^(?!.*\S{10}).*$
,但它不会验证电子邮件。
我绞尽脑汁待了十分钟没有运气,我认为这一定是我尚未学习的语法。
非常感谢所有建议,非常感谢!
答案 0 :(得分:1)
我会使用以下模式:
\b[^\s@]{30,}\b
@
后一个字。
var n = 30;
// doubly escaped slashes and global search (g)
var regex = new RegExp('\\b[^\\s@]{' + n + ',}\\b', 'g');
var text = document.getElementById('text').innerHTML;
var match = text.match(regex);
if(match) {
console.log("Document contains a value with over " + n + " characters.");
console.log(match);
}
else {
console.log("Document does not contain a value with over " + n + " characters.");
}
<div id="text">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos
et accusametjustoduodoloreseterebumstet@clita.kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla
facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet,
</div>