只替换"[old]"标签内的" "字符,并且把标签也替换为[table]如:
s="asd fs d[old]aaa sdfdsf asdfs[/old]sdfssd"
替换为:
asd fs d[table]aaa sdfdsf asdfs[/table]sdfssd试了半天也没找着北,请高手指教.

解决方案 »

  1.   


    s="asd fs d[old]aaa sdfdsf asdfs[/old]sdfssd" 
    s =s.replace(/\ /g," ")
    s =s.replace(/(\[\/?)old\]/g,"$1table]")
    alert(s)
      

  2.   

    按你给的代码,这样
    <script>
    var s="asd fs&nbsp;d[old]aaa&nbsp;sdfdsf&nbsp;asdfs[/old]sdfssd";
    var reg = /\[old\](.*?)&nbsp;(.+?)&nbsp;(.*?)\[\/old\]/gi;
    alert(s.replace(reg, '[table]$1 $2 $3[/table]'));
    </script>
      

  3.   

    2楼大哥,我标签内的"&nbsp;"数是不定的,也可以2个或3个或更多.
    你能再完善一下吗?
      

  4.   

    按你给的例子是实现不了的,或者我们没有理解?
    这里s="asd fs&nbsp;d[old]aaa&nbsp;sdfdsf&nbsp;asdfs[/old]sdfssd" 替换为: 
    asd fs&nbsp;d[table]aaa sdfdsf asdfs[/table]sdfssd 
      

  5.   

    2楼的方法可以完成我的例子.但我说的是标签内的"&nbsp;"如果有很多就没办法替换了.比如:s="1111 222&nbsp;333[old]444&nbsp;5555&nbsp;66666&nbsp;777777[/old]999"替换后:
    1111 222&nbsp;333[table]444 5555 66666&nbsp;777777[/table]999麻烦你再给看看,能做到吗?
      

  6.   

    有什么规律吗?
     比如
    s="1111 222&nbsp;333[old]444&nbsp;5555&nbsp;66666&nbsp;777777[/old]999" 
    这个替换后应该是什么样子?
      

  7.   


    应该是:1111 222&nbsp;333[table]444 5555 66666 777777[/table]999
      

  8.   

    也就是标签内的所有"&nbsp;"替换为" "
      

  9.   

    被误导了, 楼主是不是把[old][/old]里的&nbsp;换成空白然后把old换成table?
    <script>
        var s="1111 222&nbsp;333[old]444&nbsp;5555&nbsp;66666&nbsp;777777[/old]999";
        var reg = /\[old\].+?\[\/old\]/gi;
        alert(s.replace(reg, function(a){
    return a.replace(/&nbsp;/g, ' ').replace(/old/g, 'table');
    }));
    </script>
      

  10.   

    reg = /\[old\](.*?)\[\/old\]/g;
    str = "asd fs&nbsp;d[old]aaa&nbsp;bbb&nbsp;ccc&nbsp;ddd[/old]sdfssd";
    alert(str.replace(reg, function (str, p1, offset, s) { 
    return "[table]" + p1.replace(/&nbsp;/g," ") + "[/table]";
    } ) );
      

  11.   

    其实我要这个正则是想用在VBSCRIPT中的,没想到楼上给分了两步。re.Pattern="\[old\](.*?)\[\/old\]"
    strContent=re.Replace(strContent,replace("[table]$1[/table]","&nbsp;"," "))VBSCRIPT 中不可用呀!
      

  12.   

    要分两步走
    第一步把[old]之间的&nbsp换成空白字符,第二步把[old]换成table
    一步完成不了啊
    但PHP可以
      

  13.   

        srctxt = srctxt.replace( /(&amp;)/g,  "&" );
        srctxt = srctxt.replace( /(&lt;)/g ,  "<" );
        srctxt = srctxt.replace( /(&gt;)/g ,  ">" );
        srctxt = srctxt.replace( /(&nbsp;)/g ,  " " );
      

  14.   


    VBscript中不支持在replace的参数中使用函数。
    要麻烦很多。必须分多步完成。.将[old][/old]以及中间的内容取出,
    .替换内部的 &nbsp;. 
    .使用替换后的子串内容,替换原字符串.
    .替换[old][/old]
      

  15.   


    '//参数一:字符串
    '//参数二:需要替换的字符(支持正则式)
    '//参数三:需要替换成什么字符
    Function ReplaceText(str,reg,val)
    if isnull(str) or str="" then ReplaceText="":exit function
    Set regEx = New RegExp
    regEx.Pattern = reg
    regEx.IgnoreCase = True'//设置是否区分大小写
    regEx.Global = True
    ReplaceText = regEx.replace(str,val)
    Set reg=nothing
    End Function
    '//楼上各位大侠的答案已经差不多了,我来贴个函数吧
      

  16.   


    '//参数一:字符串
    '//参数二:需要替换的字符(支持正则式)
    '//参数三:需要替换成什么字符
    Function ReplaceText(str,reg,val)
        if isnull(str) or str="" then ReplaceText="sss":exit function
        Set regEx = New RegExp
        regEx.Pattern = reg
        regEx.IgnoreCase = true '//设置是否区分大小写
        regEx.Global = True
        ReplaceText = regEx.replace(str,val)
        Set reg=nothing
    End Function
    str = "asd fs&nbsp;d[old]aaa&nbsp;bbb&nbsp;ccc&nbsp;ddd[/old]sdfssd"
    Response.Write(ReplaceText(str,"\[old\](.*?)(&nbsp;)(.*?)\[/old\]","[table]$1$2$3[/table]"))