str = "aaa[code]bsbs[/code]bbb[code]hello world[/code]cccc…"
re=/(\[code\])/ig
ct=str.match(re).length
for(i=1;i<=ct;i++)
str=str.replace(/(\[code\])([^\[\]\/]*)(\[\/code\])/,"<textarea id='a"+i+"'>$2<\/textarea>");
alert(str);

解决方案 »

  1.   

    i=0;
    str = str.replace(/\[code\](.*?)\[\/code\]/ig,function($0,$1){
      i++;
      return "<textarea id='a" + i + "'>" + $1 + "</textarea>";
    })
      

  2.   

    不能这么简单的处理,如果代码里包含 "[code]" 结果就出现错误了
    51js上也没有处理这种情况
      

  3.   

    <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
    <% option explicit %>
    <!--#include virtual="include/conn.asp"-->
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <!-- DW6 -->
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>shawl.qiu template</title>
    </head><body>
    <%
    dim str:str = "aaa[code]bsbs[/code]bbb[code]hello world[/code]cccc…"
    Response.Write("<xmp>")
    dim re, resub, temp
    dim j:j=1
    set re=new RegExp
    with re
    .global=true
    .ignoreCase=true

    re.pattern="\[code\]"
    set resub=re.execute(str)
    for each temp in resub
    str=replace(str, temp, "<textarea id='a"&j&"'>", 1, 1, 1)
    j=j+1
    next

    re.pattern="\[\/code\]"
    str=.replace(str, "</textarea>")
    end with
    set re=nothing
    Response.Write(str)
    Response.Write("</xmp>")
    %>
    </body>
    </html>
      

  4.   

    这个去掉, <!--#include virtual="include/conn.asp"-->
    模板这样..我得改改