代码之家  ›  专栏  ›  技术社区  ›  Gerald Ferreira

使用javascript替换Regexp.replace中的动态值

  •  1
  • Gerald Ferreira  · 技术社区  · 16 年前

    我将Regex与asp和javascript结合使用

    <% 
    htreplace = ""+Request.Form("1")+"";
    hturl = Request.Form("thesite");
    htstring = htreplace
    htstring = htstring.replace(/,;~~~~/ig,';');
    htstring = htstring.replace(hturl,'http://www.example.net');
    %>
    

    如果我改变主意

    htstring = htstring.replace(hturl,'http://www.example.net');
    

    到

    htstring = htstring.replace(/hturl/ig,'http://www.example.net');
    

    或

    htstring = htstring.replace("/"+hturl+"/"+ig,'http://www.example.net');
    

    我无法再动态获取hturl值了?

    1 回复  |  直到 14 年前
        1
  •  4
  •   Kaze no Koe    16 年前
    var myregexp = new RegExp(hturl, "ig");
    
    htstring = htstring.replace(myregexp, 'blah');
    
    推荐文章