当前位置:编程学习 > 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> New Document </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>

 

补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,