日期:2014-05-16  浏览次数:20517 次

正则取代写法请教高手们
HTML code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gbk">
        <title>Javascript</title>
        <script type="text/javascript">
            function change(x){
                var i = 1;
                var tmp = document.getElementById("t1").value;
                document.getElementById("t2").valuet = function(){
                    return tmp.replace(/x/g, function(){
                        return x++;
                    })
                }()
            }
        </script>
    </head>
    <body>
        <textarea col="30" col="20" style="width:300px; height:400px" name="Test1" id="t1">A A A A A</textarea>
        <textarea col="30" col="20" style="width:300px; height:400px" name="Test2" id="t2"></textarea>
        <input type="button" name="ck" id="ck" onclick="change("A");" />
    </body>
</html>


上面是我的html+js代码
希望点击button后 第一个textarea区的值A A A A A取代成1 2 3 4 5然后把值放在第二个textarea区
请教如何实现此正则?

------解决方案--------------------
HTML code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gbk">
        <title>Javascript</title>
        <script type="text/javascript">
            function change(x){
                var i = 1;
                var tmp = document.getElementById("t1").value;
                document.getElementById("t2").value = tmp.replace(new RegExp(x, "ig"), function(){ return i++; });
            }
        </script>
    </head>
    <body>
        <textarea col="30" col="20" style="width:300px; height:400px" name="Test1" id="t1">A A A A A</textarea>
        <textarea col="30" col="20" style="width:300px; height:400px" name="Test2" id="t2"></textarea>
        <input type="button" name="ck" id="ck" onclick="change('A');" />
    </body>
</html>