日期:2014-05-16  浏览次数:20330 次

请教:对象还是正则表达式的问题?
<script   type= "text/javascript ">
<!--

var   obj   =  
{
a: "a1 ",
b: "b1 ",
c: "c1 "
}

var   s   =   "ka "
//alert(s.replace(/k([abc])/, 'obj[ "$1 "] '));
alert(s.replace(/k([abc])/,obj[ "$1 "]));

//-->
</script>

为什么结果为undefined?

------解决方案--------------------
<script type= "text/javascript ">
<!--

var obj =
{
a: "a1 ",
b: "b1 ",
c: "c1 "
}

var s = "ka "
//alert(s.replace(/k([abc])/, 'obj[ "$1 "] '));
alert(s.replace(/k([abc])/,function(a,b){return obj[b]}));

//-->
</script>