js替换字符串的所有示例代码

js如何替换字符串中所有。

/** 

* 替换字符串中所有 
* @param obj 原字符串 
* @param str1 替换规则 
* @param str2 替换成什么 
* @return 替换后的字符串 
*/ 
function replaceAll(obj,str1,str2){ 
var result = obj.replace(eval("/"+str1+"/gi"),str2); 
return result; 
}

例如: 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<title> js替换字符串的例子-www.jbxue.com-脚本学堂 </title> 
<meta name="Generator" content="EditPlus"> 
<meta name="Author" content=""> 
<meta name="Keywords" content=""> 
<meta name="Description" content=""> 
<script type="text/javascript"> 
function replaceAll(obj,str1,str2){ 
var result = obj.replace(eval("/"+str1+"/gi"),str2); 
return result; 
} 
alert(replaceAll("111,222,333",",","|")); 
</script> 
</head> 
<body> 
</body> 
</html>
posted @ 2013-07-24 06:39  一觉睡到天黑黑  阅读(364)  评论(0编辑  收藏  举报