Regex Flex中的正则表达式
我想检查字符串是否为空(只有空格也算为空)。如何在actionscript中组合正则表达式?模式应该类似于Regex Flex中的正则表达式,regex,actionscript-3,apache-flex,flex4.5,Regex,Actionscript 3,Apache Flex,Flex4.5,我想检查字符串是否为空(只有空格也算为空)。如何在actionscript中组合正则表达式?模式应该类似于/^\s*$/(对于单行字符串)^和$表示行的开始和结束,\s*表示匹配零个或多个空白字符。例如: var s:String = /* ... */; var allWhitespaceOrEmpty:RegExp = /^\s*$/; if (allWhitespaceOrEmpty.test(s)) { // is empty or all whitespace } else {
/^\s*$/
(对于单行字符串)^
和$
表示行的开始和结束,\s*
表示匹配零个或多个空白字符。例如:
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
}
也许正如评论员亚历山大·法伯(Alexander Farber)所指出的,一种更简单的方法是检查除空白字符以外的任何字符,空白字符在正则表达式中由\S
匹配:
var nonWhitespaceChar:RegExp = /\S/;
if (nonWhitespaceChar.test(s))
{
// is non-empty with at least 1 non-whitespace char
}
对于多行字符串,有什么区别吗?添加
(?s)
多行开关:/(?s)^\s*$/
,这样它将同时适用于单行字符串和多行字符串,对吗?单行字符串是多行字符串,其中行数1
,因此对/\s+/的检查更加简单