如reg是正则要替换的内容,
var ch = "aaa";
var reg = "/"+ch+"/ig";
var str = 'aaa bbb ccc aaa aaa';
var val = str.replace(reg,"");
alert(val);
</script>
用eval转义,你现在reg是一个字符串
正确写法
<script>
var ch = "aaa";
var reg = "/"+ch+"/ig";
var str = 'aaa bbb ccc aaa aaa';
var val = str.replace(eval(reg),"");
alert(val);
</script>