function replaceNotInHref(str, word, reword, isGlobal)
dim re
set re = new RegExp
re.Pattern = word & "(?:(?=.*<a\b)|(?!.*?<\/a>))"
re.IgnoreCase = True

re.Global = isGlobal

replaceNotInHref = re.replace(str, reword)
end function

str = "123<a href='f'>金属123</a>金属123<a href='f'>金属</a>123金属"
response.write replaceNotInHref(str, "金属", "被替换了", false)怎么替换A标签之外的字符

解决方案 »

  1.   

    上面例子想得到
    123<a href='f'>金属123</a>被替换了123<a href='f'>金属</a>123金属
      

  2.   

    如果是
    replaceNotInHref(str, "金属", "被替换了", true) 得到的是
    123<a href='f'>金属123</a>被替换了123<a href='f'>金属</a>123被替换了
      

  3.   

    \w*(<a[^>]+>[^<]*</a>)\w*
    然后将捕获的字符替换进去就好了。
      

  4.   

    得到的是这个<a href='f'>金属123</a><a href='f'>金属</a> 你要替换外边的捕获外边的就好了。
      

  5.   

    var str = "123<a href='f'>金属123</a>金属123<a href='f'>金属</a>123金属";
    alert(str.replace(/(?=[^>]*(?=<(?!\/)|$))金属/g,'橡胶'))
      

  6.   

    LZ写的这代码冒似不是JS吧,看起来像VB。
      

  7.   


    function replaceNotInHref(str, word, reword, isGlobal)
    dim re
    set re = new RegExp
    're.Pattern = "(?=[^>]*(?=<(?!\/)|$))" & word
    re.Pattern = "(?=[^>]*(?=<(?!\/)|$))" & word
    re.IgnoreCase = True

    re.Global = isGlobal

    replaceNotInHref = re.replace(str, reword)
    end function

    str = "123<a href='f'>金属123</a>金属123<a href='f'>金属</a>123金属"
    str1 = "123金属123金属1233"
    response.write replaceNotInHref(str, "金属", "被替换了", false)
    response.write "<br>"
    response.write replaceNotInHref(str1, "金属", "被替换了", true)还有没有a的情况,,
      

  8.   

    参考下
    <%
    function replaceNotInHref(str, word, reword, isGlobal)
    dim re, i, Matches, Match
    set re = new RegExp
    re.IgnoreCase = true
    re.Global = true
    re.Pattern = "(?:<a.*?>[\s\S]*?</a>)" Set Matches =re.Execute(str)

    i = 0
    For Each Match in Matches
    ReDim Preserve MyArray(i)
    MyArray(i) = Match.Value
    str = replace(str, Match.Value, "<@#"&i&"#@>")'
    i = i + 1
    Next

    re.Global = isGlobal
    re.Pattern = word

    str = re.replace(str, reword)

    if i > 0 then
    for i = 0 to ubound(MyArray)
    str = replace(str, "<@#"&i&"#@>", MyArray(i))
    next
    end if
    replaceNotInHref = str
    end function str = "123<a href='f'>金属123</a>金属123<a href='f'>金属</a>123金属"
    str1 = "123金属123金属1233"
    response.write replaceNotInHref(str, "金属", "被替换了", false)
    response.write "<br>"
    response.write replaceNotInHref(str1, "金属", "被替换了", true)
    %>
      

  9.   

    一個有點笨的方法:
    先把不需要換的地方換掉,再換需要的地方,最后再把不需要換的地方換回來 var str = "123<a href='f'>金属123</a>金属123<a href='f'>金属</a>123金属";
    str = str.replace(/(<a[^>]+>.*?)金属(.*?<\/a>)/ig, "$1xxoo$2").replace(/金属/g, '我暈!').replace(/xxoo/ig, "金属");
    alert(str);
    也可以簡化一下,先不管那么多,都換掉,然后再把不需要換的地方換回來
    看你程序怎麼做更合適