Regex 使用正则表达式将actionScript中的每个单词大写
我试图在actionScript中不使用循环来完成初始大写,但我被卡住了。我想选择第一个字母或每个单词,然后在该字母上使用大写字母。嗯,我选对了部分,但现在已经走到了尽头,有什么想法吗?我试图做到这一点,没有循环和切割字符串Regex 使用正则表达式将actionScript中的每个单词大写,regex,apache-flex,actionscript-3,air,Regex,Apache Flex,Actionscript 3,Air,我试图在actionScript中不使用循环来完成初始大写,但我被卡住了。我想选择第一个字母或每个单词,然后在该字母上使用大写字母。嗯,我选对了部分,但现在已经走到了尽头,有什么想法吗?我试图做到这一点,没有循环和切割字符串 // replaces with x since I can't figure out how to replace with // the found result as uppercase public function initialcaps():void {
// replaces with x since I can't figure out how to replace with
// the found result as uppercase
public function initialcaps():void
{
var pattern:RegExp=/\b[a-z]/g;
var myString:String="yes that is my dog dancing on the stage";
var nuString:String=myString.replace(pattern,"x");
trace(nuString);
}
尝试使用返回大写字母的函数:
myString.replace(pattern, function($0){return $0.toUpperCase();})
这至少在JavaScript中有效。尝试使用返回大写字母的函数:
myString.replace(pattern, function($0){return $0.toUpperCase();})
这至少在JavaScript中有效。您也可以使用它来避免编译器警告
myString.replace(pattern, function():String
{
return String(arguments[0]).toUpperCase();
});
您还可以使用此选项来避免编译器警告
myString.replace(pattern, function():String
{
return String(arguments[0]).toUpperCase();
});
我想我会给他们两分钱买那些可能全是帽子的绳子
var pattern:RegExp = /\b[a-zA-Z]/g;
myString = myString.toLowerCase().replace(pattern, function($0){return $0.toUpperCase();});
我想我会给他们两分钱买那些可能全是帽子的绳子
var pattern:RegExp = /\b[a-zA-Z]/g;
myString = myString.toLowerCase().replace(pattern, function($0){return $0.toUpperCase();});
这个答案不会在严格的条件下抛出任何类型的编译器错误,我希望它更加健壮,处理诸如连字符忽略它们、下划线将它们视为空格和其他特殊的非单词字符(如斜杠或点)之类的边缘情况 注意正则表达式末尾的/g开关非常重要。没有它,函数的其余部分就毫无用处,因为它只处理第一个字,而不会处理任何后续字
for each ( var myText:String in ["this is your life", "Test-it", "this/that/the other thing", "welcome to the t.dot", "MC_special_button_04", "022s33FDs"] ){
var upperCaseEveryWord:String = myText.replace( /(\w)([-a-zA-Z0-9]*_?)/g, function( match:String, ... args ):String { return args[0].toUpperCase() + args[1] } );
trace( upperCaseEveryWord );
}
输出:
This Is Your Life
Test-it
This/That/The Other Thing
Welcome To The T.Dot
MC_Special_Button_04
022s33FDs
对于复制和粘贴艺术家,这里有一个随时可以滚动的功能:
public function upperCaseEveryWord( input:String ):String {
return input.replace( /(\w)([-a-zA-Z0-9]*_?)/g, function( match:String, ... args ):String { return args[0].toUpperCase() + args[1] } );
}
这个答案不会在严格的条件下抛出任何类型的编译器错误,我希望它更加健壮,处理诸如连字符忽略它们、下划线将它们视为空格和其他特殊的非单词字符(如斜杠或点)之类的边缘情况 注意正则表达式末尾的/g开关非常重要。没有它,函数的其余部分就毫无用处,因为它只处理第一个字,而不会处理任何后续字
for each ( var myText:String in ["this is your life", "Test-it", "this/that/the other thing", "welcome to the t.dot", "MC_special_button_04", "022s33FDs"] ){
var upperCaseEveryWord:String = myText.replace( /(\w)([-a-zA-Z0-9]*_?)/g, function( match:String, ... args ):String { return args[0].toUpperCase() + args[1] } );
trace( upperCaseEveryWord );
}
输出:
This Is Your Life
Test-it
This/That/The Other Thing
Welcome To The T.Dot
MC_Special_Button_04
022s33FDs
对于复制和粘贴艺术家,这里有一个随时可以滚动的功能:
public function upperCaseEveryWord( input:String ):String {
return input.replace( /(\w)([-a-zA-Z0-9]*_?)/g, function( match:String, ... args ):String { return args[0].toUpperCase() + args[1] } );
}
嗨,秋波,谢谢你的回复。这是完美的,它只是为匿名函数树立了一个标志,没什么大不了的。如果你不介意我问的话。0美元是多少?美元似乎不在AS3文档中。那么这是如何工作的呢?@Deyon:$0只是一个正则变量标识符。你也可以用火柴或任何你想要的东西。但是因为replace使用$1、$2等来引用组的匹配,所以$0是整个匹配的好名字。嗨,Gumbo,谢谢你的回复。这是完美的,它只是为匿名函数树立了一个标志,没什么大不了的。如果你不介意我问的话。0美元是多少?美元似乎不在AS3文档中。那么这是如何工作的呢?@Deyon:$0只是一个正则变量标识符。你也可以用火柴或任何你想要的东西。但是,由于replace使用$1、$2等来引用组的匹配项,因此,0是整个匹配项的好名称。