简体   繁体   中英

Regex how can I check that message isn't empty

How can I check if a string isn't empty using regex? For example, if the user give a lot of space characters whitout letters, it will be considered as empty

I've tried [a-zA-Z0-9] to check if there's a letter in the string, but this don't work.

您可以使用此正则表达式检测到空字符串(包含零个或多个空格)

^\s*$

You don't even have to use Regex if you are allowing everything other than empty string, you can just use trim() which will trim white spaces.

Boolean("       ".trim()); //false
Boolean("     b ".trim()); //true
Boolean("\n\t\r ".trim()); //false
Boolean("\n\th\r".trim()); //true

You can check to see if it contains any non-whitespace (ie printing) characters:

\S

That's backslash capital S .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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