我正在开发一个涉及创建Adobe Flash应用程序的项目,该应用程序可以更改输入文本框中文本的时态。 原始示例文本是: “亚特兰蒂斯的城市都将丢失。狼人是罪魁祸首。我感到难过等等都是我的朋友。居民和亚特兰蒂斯不高兴了。这是一种浪费。”
我已经想出如何让它过去紧张并且大部分都是紧张的。但我似乎无法弄清楚如何将“我是”的部分从过去时态改为现在时(我)。请帮忙。
original_btn.addEventListener(MouseEvent.CLICK, ConvertOriginal);
past_btn.addEventListener(MouseEvent.CLICK, ConvertPast);
present_btn.addEventListener(MouseEvent.CLICK, ConvertPresent);
function ConvertOriginal(e:MouseEvent):void {
body_txt.text = "The city of Atlantis is lost. The werewolves were to blame. I am saddened and so were my friends. The residents and Atlantis are unhappy too. It is such a waste.";
}
function ConvertPast(e:MouseEvent):void {
var myPattern1:RegExp = /\s(is|am)\s/g;
var str:String = body_txt.text;
body_txt.text = body_txt.text.replace(myPattern1, " was ");
var myPattern2:RegExp = /\sare\s/g;
body_txt.text = body_txt.text.replace(myPattern2, " were ");
}
function ConvertPresent(e:MouseEvent):void {
var myPattern1:RegExp = /\swas\s/g;
var str:String = body_txt.text;
body_txt.text = body_txt.text.replace(myPattern1, " is ");
var myPattern2:RegExp = /was/g;
body_txt.text = body_txt.text.replace(myPattern2, " I am ");
var myPattern3:RegExp = /\swere\s/g;
body_txt.text = body_txt.text.replace(myPattern3, " are ");
}
答案 0 :(得分:0)
您需要使用此正则表达式替换顺序:
\bI was\b
与I am
\bwas\b
与is
\bwere\b
与are
以下是您修改后的功能:
function ConvertPresent(e:MouseEvent):void {
var myPattern1:RegExp = /\bI was\b/g;
var str:String = body_txt.text;
body_txt.text = body_txt.text.replace(myPattern1, "I am");
var myPattern2:RegExp = /\bwas\b/g;
body_txt.text = body_txt.text.replace(myPattern2, "is");
var myPattern3:RegExp = /\bwere\b/g;
body_txt.text = body_txt.text.replace(myPattern3, "are");
}
答案 1 :(得分:0)
您可以使用字符串替换方法进行转换,如下所示
private function replaceAll(pattern:*,replacementStr:String,originalString:String):String {
if (originalString!= null) {
while (originalString.search(pattern) != -1) {
originalString= originalString.replace(pattern,replacementStr);
}
}
return originalString;
}