Actionscript中的ReplaceAll函数

时间:2014-03-20 21:19:59

标签: string actionscript-3 flash

在Actionscript中有替代方法吗?例如:

Array[hi,tom,fcuk,xes]

SomeString.replace(Array,"*")

基本上,它应该用*。

替换所有数组字

谢谢!

2 个答案:

答案 0 :(得分:0)

var somestring:String = "a111b222c333";
var replacelist:Array = ["a","b","c"];

for each(var str:String in replacelist)
{
while (somestring.search(str) != -1) somestring = somestring.replace(str, "*");
}

trace (somestring);  // *111*222*333

答案 1 :(得分:0)

  

基本上,它应该用*。

替换所有数组字

简单快速的迭代将帮助您;)

function replaceAll(list:Array, value:*):void {
    var i:uint, len:uint = list.length;

    for (i; i < len; ++i) {
        //any logic you want go here...
        list[i] = value;
    }
}

var test: Array = ["hi","tom","fcuk","xes"];
replaceAll(test, "*")
trace(test); //*,*,*,*
  

替换现有字符串中的单词

//Words to replace, use word boundary to chop full words (\b + wordToSearch + \b)
//Or construct it from Array
var badWords: RegExp =  /Lorem|ipsum|\bin\b/g;
var text: String = "Lorem ipsum dolor sit amet, vix te purto eros inimicus, per omnes admodum assentior ne, vis ei autem affert. At adhuc splendide definiebas per, dico soluta aliquid id mea. Agam probo nostrum mel in. Dicunt splendide ex eum. Te viris minimum elaboraret cum. Sed ea essent vidisse vituperata.";
trace(text.replace(badWords, "***"));

删除网址:

var urlPattern: RegExp = /(https?:\/\/)?(www\.)?([a-zA-Z0-9_%]*)\b\.[a-z]{2,4}(\.[a-z]{2})?((\/[a-zA-Z0-9_%]‌​*)+)?(\.[a-z]*)?/gi;
var someTextWithUrls: String = "Well, the way someurl.com they make shows is, http://simpleurl.it they make one show. That shows called a pilot. isiturl.im";
trace(someTextWithUrls.replace(urlPattern, "***")); //Well, the way *** they make shows is, *** they make one show. That shows called a pilot. ***