简体   繁体   English

正则表达式最小字数

[英]regular expression minimum word count

What regex should I use for a minimum word count? 最少字数应使用什么正则表达式? This is for a dating website where the user has to describe himself in minimum 50 words. 这是针对约会网站的,用户必须至少用50个字来形容自己。 So can allow all punctuation characters, new lines, question marks, numbers and basically anything the user types in as long as it has over 50 words. 因此可以允许所有标点符号,换行符,问号,数字以及基本上所有用户键入的内容,只要它超过50个字即可。

I have this (\\b[A-Za-z0-9 '-_]+\\b.*){50,} but it does not let you do new line characters 我有这个(\\b[A-Za-z0-9 '-_]+\\b.*){50,}但它不允许您换行

Thanks 谢谢

If it's a website as you say so, you don't need regex. 如果这是您所说的网站,则不需要正则表达式。 Though it is not tagged with , I do suppose you'll find it helpful. 尽管它没有用标记,但我想您会发现它很有帮助。 What you can do is 你能做的是

var elem = document.getElementById("id of the text input element here");
if(elem.value.split(" ").length < 50) {
   alert("Minimum 50 words please!!");
}

要包括换行符,请使用此正则表达式:

(\b[A-Za-z0-9\s'_-]+\b){50,} 

@DRC your solution does not solve the problem of users entering 50 spaces @DRC您的解决方案无法解决用户输入50个空格的问题

I tweaked your solution and got this working for me 我调整了您的解决方案,并为我工作了

function min50Words(source, arguments)
    {
        if (arguments.Value.replace(/\s+/g, ' ').split(" ").length < 50) {
            arguments.IsValid = false;
        } else {
            arguments.IsValid = true;
        }
    }

This bit 这一点

.replace(/\s+/g, ' ').split(" ").length < 50

will collapse all spaces into a single space 将所有空间折叠成一个空间

Thanks for all your help guys 谢谢你们的帮助

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

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