0

我已经研究了几天的这个 ActionScript 代码现在在 JavaScript 中可以 100% 正常工作,但是当我尝试在 ActionScript 中编译它时,它说我有意外的 /、) 和 } 符号。这种语法是否错误,如果是,我应该如何解决?我想我可以将它作为 Javascript 进行测试,以便使用http://jsfiddle.net/进行更快的测试,但现在我喜欢 =(

var txt = "This is a [rainbow]test to show that I can[/rainbow] make whatever I want [rainbow]appear as a rainbow[/rainbow] because I am [rainbow]awesome[/rainbow].";


    if ((txt.indexOf("[rainbow]") > -1) && (txt.indexOf("[/rainbow]") > -1)) {
        var colors = ['f0f', 'f0c', 'f09', 'f06', 'f03', 'f00', 'f30', 'f60', 'f90', 'fc0', 'ff0', 'cf0', '9f0', '6f0', '3f0', '0f0', '0f3', '0f6', '0f9', '0fc', '0ff', '0cf', '09f', '06f', '03f', '00f', '30f', '60f', '90f', 'c0f'];

        function rainbowify(text) {
            return text.replace(/\[rainbow\](.+?)\[\/rainbow\]/g, function(_, inner) {
                return inner.replace(/./g, function(ch, i) {
                    return '<font color="#' + colors[i % colors.length] + '">' + ch + '</font>';
                });
            })
        }
        txt = rainbowify(txt);
        document.write(txt);
    }​
4

1 回答 1

0

嗯,就是这样:

    txt = txt.replace("&apos;", "@");
if ((txt.indexOf("[rainbow]") > -1) && (txt.indexOf("[/rainbow]") > -1)) {
    var firstChar = txt.indexOf("[rainbow]") + 9;
    var lastChar = txt.indexOf("[/rainbow]");

    while (lastChar <= txt.lastIndexOf("[/rainbow]")) {
        var RAINBOWTEXT = '';
        var i = firstChar;
        while (i < lastChar) {
            RAINBOWTEXT += txt.charAt(i);
            i++
        }
        var text = RAINBOWTEXT;
        var texty = '';

        colors = new Array('ff00ff','ff00cc','ff0099','ff0066','ff0033','ff0000','ff3300','ff6600','ff9900','ffcc00','ffff00','ccff00','99ff00','66ff00','33ff00','00ff00','00ff33','00ff66','00ff99','00ffcc','00ffff','00ccff','0099ff','0066ff','0033ff','0000ff','3300ff','6600ff','9900ff','cc00ff');

        i = 0;

        while (i <= text.length) {
            var t = text.charAt(i);

            if (t != undefined) {
                texty += "<font color=\"#" + colors[i % colors.length] + "\">" + t + "</font>";
                i++;
            }
        }

        texty = texty.replace("> <", ">&nbsp;<");
        var REPLACEME = "[rainbow]" + RAINBOWTEXT + "[/rainbow]";
        txt = txt.replace(REPLACEME, texty);

        if (lastChar == txt.lastIndexOf("[/rainbow]")) {
            break;
        }
        nextChar = lastChar + 10;
        firstChar = txt.indexOf("[rainbow]", lastChar) + 9;
        lastChar = txt.indexOf("[/rainbow]", lastChar);
    }
}
txt = txt.replace("@", "&apos;");
于 2012-05-24T20:38:15.240 回答