简体   繁体   中英

Regular expression in Flex

I want to check if the string is not empty (having whitespaces only also counts as empty). How to compose the regular expression in actionscript?

The pattern should be something like /^\\s*$/ (for a single line string); ^ and $ represent the start and end of the line and \\s* means match zero or more whitespace characters. For example:

var s:String = /* ... */;
var allWhitespaceOrEmpty:RegExp = /^\s*$/;
if (allWhitespaceOrEmpty.test(s))
{
    // is empty or all whitespace
}
else
{
    // is non-empty with at least 1 non-whitespace char
}

Perhaps a simpler way as commenter Alexander Farber points out is to check for any character except a whitespace character, which is matched by \\S in regex:

var nonWhitespaceChar:RegExp = /\S/;
if (nonWhitespaceChar.test(s))
{
    // is non-empty with at least 1 non-whitespace char
}

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